-
RoutingJavascript/Node.js 2020. 1. 6. 17:25
라우팅
라우팅은 URI 및 특정한 HTTP 요청 메소드인 특정 엔드포인트에 대한 클라이언트 요청에 애플리케이션이 응답하는 방법을 결정하는 것을 말한다. 각 라우트는 하나 이상의 핸들러 함수를 가질 수 있으며, 이러한 함수는 라우트가 일치할 때 실행됩니다.
라우트 예시
package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters{ "name": "dyeon-restful-project", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "cookie-parser": "~1.4.4", "debug": "~2.6.9", "express": "~4.16.1", "http-errors": "~1.6.3", "jade": "~1.11.0", "jsonwebtoken": "^8.5.1", "morgan": "~1.9.1", "promise-mysql": "^4.1.1" } } npm start를 실행하면, node ./bin/www가 실행 되는 것을 알 수 있다.
www
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#!/usr/bin/env node /** * Module dependencies. */ var app = require('../app'); var debug = require('debug')('dyeon-restful-project:server'); var http = require('http'); /** * Get port from environment and store in Express. */ var port = normalizePort(process.env.PORT || '3000'); app.set('port', port); /** * Create HTTP server. */ var server = http.createServer(app); /** * Listen on provided port, on all network interfaces. */ server.listen(port); server.on('error', onError); server.on('listening', onListening); /** * Normalize a port into a number, string, or false. */ app 변수를 이용해 app.js를 가져오고, port를 지정하고 server를 구동하는 코드가 있다.
app.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersvar createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); var indexRouter = require('./routes/index'); var app = express(); /* ... */ app.use('/', indexRouter); app.js에서 주목해야 할 곳은 app.use이다. index.js 파일은 '/'에 연결되고, users.js는 '/users'에 연결된다.
index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters// router.post('/', authUtil.validToken, async (req, res) => { router.post('/', async (req, res) => { const {userIdx, title, content, active} = req.body; // search miss parameters if(!userIdx || !title || !content || !active){ const missParameters = Object.entries({userIdx, title, content, active}) .filter(it => it[1] == undefined).map(it => it[0]).join(','); res.status(statusCode.BAD_REQUEST).send(utils.successFalse(responseMessage.X_NULL_VALUE(missParameters))); return; } request를 받아서 method에 따라서 response를 처리하는 로직이 있다.
출처
https://expressjs.com/ko/starter/basic-routing.html
Express 기본 라우팅
기본 라우팅 라우팅은 URI(또는 경로) 및 특정한 HTTP 요청 메소드(GET, POST 등)인 특정 엔드포인트에 대한 클라이언트 요청에 애플리케이션이 응답하는 방법을 결정하는 것을 말합니다. 각 라우트는 하나 이상의 핸들러 함수를 가질 수 있으며, 이러한 함수는 라우트가 일치할 때 실행됩니다. 라우트 정의에는 다음과 같은 구조가 필요합니다. app.METHOD(PATH, HANDLER) 여기서, app은 express의 인스턴스입니다. METHOD는
expressjs.com
SOPT 25th Server Seminar PPT
'Javascript > Node.js' 카테고리의 다른 글
cookie & session (0) 2020.01.21 Node.js 란? (0) 2020.01.21 Passport (0) 2020.01.15 Single Thread (0) 2020.01.06 비동기 (0) 2020.01.06