|
|
|
import * as express from "express";
|
|
|
|
import * as cors from "cors";
|
|
|
|
|
|
|
|
import { CatType, Cat } from "./app.model";
|
|
|
|
|
|
|
|
const app: express.Express = express();
|
|
|
|
const port: number = 8000;
|
|
|
|
|
|
|
|
app.use(cors());
|
|
|
|
|
|
|
|
// 전체 적용
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
console.log(req.rawHeaders[1]);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// 특정 REST 엔드포인트 적용
|
|
|
|
app.get("/cats/blue", (req, res) => {
|
|
|
|
console.log("This is /cats/blue GET middleware");
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/", (req: express.Request, res: express.Response) => {
|
|
|
|
res.send({ cats: Cat });
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/cats/blue", (req, res) => {
|
|
|
|
res.send({ blue: Cat[0] });
|
|
|
|
});
|
|
|
|
app.get("/cats/som", (req, res) => {
|
|
|
|
res.send({ som: Cat[1] });
|
|
|
|
});
|
|
|
|
|
|
|
|
// not found
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
console.log(req.rawHeaders[1]);
|
|
|
|
console.log("This is 404 middleware");
|
|
|
|
res.send({ error: "404 not found error" });
|
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`server is on ${port}`);
|
|
|
|
});
|