chapter 4
μμ μλ²λ₯Ό ꡬμΆν λ, μ²μλΆν° λͺ¨λ κ²μ κ°λ°ν κ²μ΄ μλλΌ const http = require("http")
λ₯Ό ν΅ν΄ http
λΌλ ν¨ν€μ§λ₯Ό μ€μΉνμ¬ μλ² κ΅¬μΆμ νΈλ¦¬νκ² νλ€.package.json
# npmμ μ¬μ©ν μ μλ μ΄κΈ° νκ²½ ꡬμΆ
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (npm_test) npm_test
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author: bori
license: (ISC)
About to write to C:\Users\User\Desktop\likelion\μ½λλΌμ΄μΈ\μΌλ¨ λ§λλ Node.js\js_test\npm_test\package.json:
{
"name": "npm_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "bori",
"license": "ISC"
}
Is this OK? (yes)
npm_test
ν΄λμ package.json
νμΌ μμ±{
"name": "npm_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "bori",
"license": "ISC"
}
scripts
: npmμ λν λͺ
λ Ήμ΄λ₯Ό λ£μ μ μλ λΆλΆhello
λͺ
λ Ήμ΄λ₯Ό μΆκ° ν μ€ν "scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"hello": "echo hello"
},
$ npm run hello
> npm_test@1.0.0 hello
> echo hello
$ npm install [ν¨ν€μ§λͺ
]
# ν¨ν€μ§ μ€ νλμΈ express μ€μΉ
$ npm install express
package.json
νμΌ λ΄ dependencies
κ° μΆκ°λλ©΄μ μΆκ°λ ν¨ν€μ§ λͺ
κ³Ό λ²μ μ΄ λνλλ€. "dependencies": {
"express": "^4.17.2"
}
package-lock.json
: μ€μΉν λͺ¨λ ν¨ν€μ§μ λ²μ μ λ³΄κ° λ΄κΉnode_modules
ν΄λ : λͺ¨λ μμ‘΄μ±μ΄ μλ ν¨ν€μ§λ₯Ό μ΄ ν΄λ λ΄μμ κ΄λ¦¬express
ν¨ν€μ§ ν κ°λ§ μ€μΉν΄λ λ€μν ν¨ν€μ§(μμ‘΄μ±)λ€μ΄ κ°μ΄ μ€μΉ$ npm install -D nodemon
-D
: κ°λ°νκ²½μμλ§ μ¬μ©νκ² λ€λ μλ―Έ-D
μ΅μ
μ¬μ© "devDependencies": {
"nodemon": "^2.0.15"
}
-g
: κΈλ‘λ² μ΅μ
-g
μ΅μ
μ ν΅ν΄ μ 체μ μΌλ‘ μ€μΉλΉ λ₯΄κ³ κ°νΈν μΉ νλ μμν¬
μμ²μ΄ λ€μ΄μμ λ
κΈ°μ‘΄ : req.url νμ± => req.method νμΈ => 쿼리문 νμ±μ κ³Όμ μ κ±°μΉ¨
express : req.get('κ²½λ‘')μ κ°μ΄ ν΄λΉ λ©μλλ₯Ό μ μ΄μ λ°λ‘ νμΈμ΄ κ°λ₯ => req.queryλ‘ μμ²μ λ§λ 쿼리문μ νμΈν μ μλ€.
νλ μμν¬ : νλ‘κ·Έλ¨μ λ§λ€κΈ° μν κΈ°λ³Έ ν
=> 체κ³μ μΈ μ½λκ΄λ¦¬ μ μ§λ³΄μ μ©μ΄
μλμ°¨λ₯Ό λ§λ λ€κ³ κ°μ
μλμ°¨ μμ λΆν : λ°ν΄, μμ§, νΈλ€ λ±
λΆνλ€μ μλ¬΄λ° κΈ°μ€ μμ΄ μλμ°¨λ₯Ό λ§λ€κ² λλ©΄ λ°ν΄λ μ΄λμ λ¬μμΌ νλμ§, μμ§μ μ΄λ»κ² ν΄μΌνκ³ , νΈλ€μ μ΄λ€ λͺ¨μμ΄μ΄μΌ νλμ§ μ μμ μ
μ₯μμλ μ΄λ €μ΄ λΆλΆμ΄λ€.
=> μ΄ λΆλΆμ ν΄μνκΈ° μν΄ μλμ°¨ νλ μμ λ§λ€μ΄μ κ° λΆνλ€μ΄ μ΄λμ μμΉν μ μκ³ , μ΄λ€ λͺ¨μμ΄μ΄μΌ νλμ§ κ°μμ μ
λ§μ λ§κ² λ£μ΄μ£ΌκΈ°λ©΄ νλ©΄ μλμ°¨κ° λΉ λ₯΄κ² μμ±
μλμ°¨ νλ μ = νλ μμν¬
expressμ κΈ°λ³Έ ꡬ쑰λ₯Ό λ§λ€μ΄ μ‘°κΈ λ κ°νΈνκ² κ°λ°μ ν μ μλ€.
express-generatorλ₯Ό μ€μΉνλ©΄ λ€μκ³Ό κ°μ λλ ν λ¦¬κ° μμ±λλ€
bin
ν΄λ > www
νμΌ : μλ²λ₯Ό μ€ννλ νμΌ, ν¬νΈλ²νΈλ₯Ό μ§μ κ°λ₯public
ν΄λ : image, js, css λ± λ¦¬μμ€κ° ν¬ν¨λ¨routes
ν΄λ : νμ΄μ§ λΌμ°ν
κ³Ό κ΄λ ¨λ νμΌ μ μ₯, μ€μ μλ² λ‘μ§ μμ±views
ν΄λ : μ΄ ν΄λ λ΄μ νμΌλ€μ ν
νλ¦Ώ νμΌμ΄λΌκ³ λͺ
λͺ
. ν
νλ¦Ώ νμΌμ μ’
λ₯μλ jade, ejs λ±μ΄ μλ€.<%= μλ°μ€ν¬λ¦½νΈ μ½λ %>
μ νμμΌλ‘ HTML μ½λ λ΄μ JavaScript μ½λλ₯Ό μμ±ν μ μλ€.app.js
: ν΅μ¬μ μΈ μλ² μν . λΌμ°ν
μ μμμ # μ΄λ κ² μ¬λ¬ ν¨ν€μ§λ₯Ό ν λ²μ μ€μΉ κ°λ₯
$ npm install -g express express-generator nodemon
express
λͺ
λ Ήμ΄λ₯Ό μ
λ ₯ν ν--ejs
: ν
νλ¦Ώ νμ
μ€μ first-project
: νλ‘μ νΈ λͺ
$ express --ejs first-project
# μ€μΉ ν ν΄λΉ λλ ν λ¦¬λ‘ μ΄λ
$ cd .\first-project
# νμ ν¨ν€μ§ μ€μΉ
$ npm install
Cannot find module 'http-errors'
μλ¬ λ°μ# μλ² μ€ν
$ npm start
> first-project@0.0.0 start
> node ./bin/www
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module 'http-errors'
npm install
μ μ€νν ν npm start
λ€μ μ€νhttp://localhost:3000/
λ‘ μ μ : expressκ° λνλλ€# nodemonμ μ΄μ©ν΄ μλ² μ€ν
$ nodemon ./bin/www
nodemon
μ λ³κ²½μ¬νμ΄ μμ λ, μλ²λ₯Ό κ»λ€/μΌ°λ€ νμ§ μμλ λ³κ²½μ¬νμ΄ μ€νλ μ μλλ‘ λμμ€λ€