2021年2月10日再開(TIL Express)
18849 ワード
Express
https://www.youtube.com/watch?v=pKd0Rpw7O48
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello Wolrd!!!");
});
app.get("/api/courses", (req, res) => {
res.send([1, 2, 3]);
});
/// /api/course/1
app.get("/api/posts/:year/:month", (req, res) => {
res.send(req.params);
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listeining on port ${port}...`));
app.get("/api/posts/:year/:month", (req, res) => {
res.send(req.query);
});
app.get("/api/courses/:id", (req, res) => {
const course = courses.find((c) => c.id === parseInt(req.params.id));
if (!course)
res.status(404).send("The course with the given ID was not found.");
res.send(courses);
});
app.post("/api/courses", (req, res) => {
const course = {
id: courses.length + 1,
name: req.body.name,
};
courses.push(course);
res.send(course);
});
app.post("/api/courses", (req, res) => {
if (!req.body.name || req.body.name.length < 3) {
//400 Bad Request
res.status(400).send("Name is required and should be minimum 3 characters");
return;
}
const course = {
id: courses.length + 1,
name: req.body.name,
};
courses.push(course);
res.send(course);
});
Invalidation Check
const Joi = require("joi");
const express = require('express');
// const port = 3000;
const app = express()
app.set('port', process.env.PORT || 3000);
app.use((req, res, next) => {
console.log('요청받은 url: ' + req.url)
next()
})
/* eslint no-unused-vars: 0 */
app.get('/', (req, res, next) => {
res.send('OK')
next()
}, (req, res) => {
throw new Error('error')
})
app.use((err, req, res, next) => {
console.error(err)
res.status(500).send('서버에서 에러가 발생했습니다')
})
app.listen(app.get('port'), () => {
console.log('서버 시작')
})
ジョン様からのテンプレートReference
この問題について(2021年2月10日再開(TIL Express)), 我々は、より多くの情報をここで見つけました https://velog.io/@jtlim0414/2021년-2월-10일-복기TIL-Expressテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol