app.use(express.json()); 적용 후, SyntaxError
571
작성한 질문수 1

app.use(express.json());
jsonParser기능을 하는 미들웨어 추가 이후,
postman에서 request를 보냈는데,
정상적으로 터미널에 출력이 되지 않습니다.
무엇이 문제일까요?
하단에 app.ts 코드를 첨부합니다.
import * as express from "express";
import { Cat, CatType } from "./app.model";
const app: express.Express = express();
app.use((req, res, next) => {
console.log(req.rawHeaders[1]);
console.log("this is logging middlewre");
next();
});
//* json middleware
app.use(express.json());
//* READ 고양이 전체 데이터 다 조회
app.get("/cats", (req, res) => {
try {
const cats = Cat;
// throw new Error("db connect error");
res.status(200).send({
success: true,
data: {
cats,
},
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
});
//* READ 특정 고양이 데이터 조회
app.get("/cats/:id", (req, res) => {
try {
const params = req.params;
const cat = Cat.find((cat) => {
return cat.id === params.id;
});
// throw new Error("db connect error");
res.status(200).send({
success: true,
data: {
cat,
},
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
});
//* CREATE 새로운 고양이 추가 api
app.post("/cats", (req, res) => {
try {
const data = req.body;
console.log(data);
res.status(200).send({
success: true,
data: {},
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
});
//* 404 middleware
app.use((req, res, next) => {
console.log("this is error middleware");
res.send({ error: "404 not found error" });
});
app.listen(8000, () => {
console.log("server is on...");
});
답변 1
프로젝트 환경 세팅할 때 최신 노드 버젼을 사용하시는 분들은 참고하셔도 좋을 것 같아요~
2
81
1
DTO에 대한 질문
1
88
2
백엔드 MVC에서 View의 역할은 무엇인가요?
1
95
2
추가 업데이트 관련 건
0
92
2
nest js 버전문제
0
81
2
mongdb 스키마 공식 문서와 형태가 다른 이유 궁금합니다.
0
103
1
라인 끝에 에러 표시(eslint) 때문에 구글 찾아 보니.
0
76
1
전체 고양이 조회 라우터 중 error.message 오류
0
70
1
캡슐화 추가 설명 중 단일책임원칙 관련 질문
0
106
0
42강 고양이끼리 소통 댓글 구현 중 Schema hasn't been registered for model 'comments' 에러 해결
0
82
1
채팅 이슈
0
134
1
모듈이 더 이상 지원하지 않는답니다
0
207
1
오류가 있습니다
0
106
1
import 에서 오류가 납니다
0
128
1
이런 오류가 나옵니다
0
101
1
에러가 발생합니다
0
111
1
프론트 에러 뜨는데 수정 안해주시나요
0
159
1
emit() broadcast.emit() 질문있습니다
0
103
1
서버연결이 안됩니다.
1
403
1
[PM2][ERROR] Command not found
0
521
1
S3에 업로드까지는 성공했는데 사진이 나오지 않습니다.
0
248
1
error_code : Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.ts(2339)
0
603
1
jwt를 따로 연습하고 있는데 env를 못읽는 것 같습니다.
0
324
2
Ec2로 안하시는 이유가 있을까요?
0
343
1






저도 같은 상황이었는데 text로 되어있어서 안되는거였어요.