// 404
app.use((req, res, next) => {
let error = new Error("Route not found!");
error.status = 404;
next(error);
});
// all other errors
app.use((error, req, res, next) => {
res.status(error.status || 500).json({
message: error.message
});
});
http://localhost:3000/api/posts/1988/10
app.get("/api/posts/:year/:month", (req, res) => {
res.status(200).send(req.params);
});
result:
{
"year": "1988",
"month": "10"
}
http://localhost:3000/api/articles?sortBy=name
app.get("/api/articles", (req, res) => {
res.status(200).send(req.query);
});
result:
{
"sortBy": "name"
}
res.header('x-auth-token', token).send('user created')
const token = req.header('x-auth-token')
app.use(helmet()); // needs to be installed first
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan("tiny")); // needs to be installed first
app.use(express.static("public")); // static files goes in public dir