Express 1. Hello World
https://expressjs.com/en/starter/hello-world.html
Express "Hello World" example
Hello world example Embedded below is essentially the simplest Express app you can create. It is a single file app — not what you’d get if you use the Express generator, which creates the scaffolding for a full app with numerous JavaScript files, Jade temp
expressjs.com
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('/로 들어왔습니다.'));
app.get('/page', (req, res) => res.send('/page로 들어왔습니다.'));
app.listen(3000, () => console.log('Example app listening on port 3000!'));
톺아보기
const express = require('express');
express 모듈을 불러온다.
const app = express();
express를 함수처럼 호출한다. express가 함수라는 것을 알 수 있다. 이것은 application 객체를 반환한다.
https://expressjs.com/en/5x/api.html#app
Express 5.x - API Reference
Express 5.x API Note: This is early alpha documentation that may be incomplete and is still under development. express() Creates an Express application. The express() function is a top-level function exported by the express module. const express = require(
expressjs.com
app.get('/', (req, res) => {
res.send('/로 들어왔습니다.')
});
app.get('/page', (req, res) => res.send('/page로 들어왔습니다.'));
app.get(path, callback(...))으로 첫번째 인자로 패스, 두번째 인자로 콜백함수를 받는다. 이것을 route 또는 routing 이라고 하는데 패스경로마다 적절한 함수를 등록해서 해당 패스경로로 사용자가 들어왔을 때 콜백함수가 실행된다.
if(pathname === '/') {~~~}와 동일하지만 훨씬 간결하고 좋은 코드이다.
app.listen(3000, () => console.log(''Example app listening on port 3000!'));
3000번 포트에서 listening을 하고 listen에 성공하면 등록한 콜백함수가 실행된다.