Server/Node.js

NodeJS - JSON null필드를 Response에서 생략하는 방법!

JaeHoney 2021. 12. 21. 08:56
 const app = express();

// JSON null 필드 생략
app.set('json replacer',  (k, v) => (v === null ? undefined : v));

 

위와 같은 방법으로, 익스프레스에 json replacer를 정의해서 필요한 경우 null필드를 response에서 제외할 수 있습니다.

 

ORM으로 sequelize나 type orm을 사용하는 경우에는 exclude, attributes 등을 사용해서 response를 원하는 범위로 조작할 수도 있습니다!