스터디코딩
[Express] 본문
순수한 node.js 기능만 가지고 직접 웹 애플리케이션을 구현하는 것은 다소 불편하다.
-> 웹프레임워크를 만들기 시작.
웹프레임워크란?
반복적으로 어디에서나 등장하는 일들을 처리할 때 더 적은 코드와 지식으로도 더 많은 일을 보다 안전하게 처리할 수 있도록 도와주는 도구
Express
: nodejs에서 가장 보편적으로 사용되는 프레임워크 중에 하나.
Routing(라우팅)
express 사용할 때 라우팅하는 법.
//route,routing : 사용자들이 여러가지 패스를 통해서 들어올 때 패스마다 적당한 응답을 해주는 것.
// '/' 경로로 들어왔을 때 호출될 함수.
/*app.get('/', function(req, res){
return res.send('Hello World!')
});*/
app.get('/', (req, res) => {
res.send('/')
})
app.get('/page', (req, res) => {
res.send('page')
})
//3000번 포트를 리스닝하게되고 성공하면 뒤에 코드 실행되게 약속.
app.listen(3000, function(){
console.log(`Example app listening at http://localhost:${port}`)
});
express 프레임워크를 사용하지 않을 때, 라우팅 하는 방법.
if(pathname === '/'){
if(queryData.id === undefined){
fs.readdir('./data', function(error, filelist){
var title = 'Welcome';
var description = 'Hello, Node.js';
var list = template.list(filelist);
var html = template.HTML(title, list,
`<h2>${title}</h2>${description}`,
`<a href="/create">create</a>`
);
response.writeHead(200);
response.end(html);
});
} else {
Route Parameter
// page/34, 로 들어왔을때 34는 pageId.
app.get('/page/:pageId', (req, res) => {
res.send(req.params) //pageId값은 req.params에 들어가있다.
})
'nodejs' 카테고리의 다른 글
[node.js / s3] node 백엔드에서 s3로 이미지 업로드(multer) (0) | 2022.02.17 |
---|---|
[node.js/express] 라우터 분리하기 (0) | 2021.12.05 |
[Node.js] Request param,query, body 의 차이점 (0) | 2021.12.04 |
Nodejs에서 동기와 비동기 1 (0) | 2021.11.28 |
javascript 3 (0) | 2021.11.28 |
Comments