1. Express
Node.js Framework Express
Node.js μ λνμ μΈ μΉνλ μμν¬μΈ Express λ₯Ό νμ©νμ¬ κ°λ¨ν μλ²λ₯Ό λ§λ€μ΄λ³Έλ€.
νλ‘μ νΈ ν΄λ λ§λ€κ³ , ν΄λΉ ν΄λμ μ§μ
νκΈ°
mkdir express-demo-app
cd mkdir express-demo-app
npm μ΄κΈ°ν
npm init -y
.gitignore νμΌ μμ±
touch .gitignore
νμΌ μμ± ν .gitignore νμ΄μ§μμ VisualStudioCode, node ν€μλ μ λ ₯ ν copy&paste νλ€. node_modules, dist κ° λ°λμ μ‘΄μ¬ν΄μΌ νλ€.
μλμ λͺ¨λ μ€μΉ
npm i -D typescript
npm i -D ts-node
npm i -D nodemon
npm i eslint
npm i express
npm i @types/express
nodemon μ κ°λ° νΈμμ μ€μΉ. ts-node μ λνμ¬ μμ‘΄μ±μ κ°μ§κ³ μκΈ° λλ¬Έμ ts-node κ° λ°λμ μ€μΉλμ΄μμ΄μΌ νλ€.
κ°μ’
μ΄κΈ°ν
# tsconfig.json νμΌ μμ±
npx tsc --init
# lint μ€μ . .eslintrc.js νμΌ μμ±
npx eslint --init

Prettier κ° extension μΌλ‘ μ€μΉλμ΄ μλ κ²½μ°, lint μ€μ ν μΆ©λ νΌνκΈ°
npm install --save-dev eslint-config-prettier
// .eslintrc.js
...
overrides: [
{
env: {
node: true,
},
files: ['.eslintrc.{js,cjs}'],
parserOptions: {
sourceType: 'script',
},
},
{
extends: ['xo-typescript', 'prettier'], // <- Add here!
files: ['*.ts', '*.tsx'],
},
],
ts-node, nodemon μ€ν λ°©λ²
npx ts-node
npx nodemon <path>
package.json μμ
...
"main": "app.ts",
"scripts": {
"start": "nodemon app.ts",
"lint": "eslint --fix .",
"test": "echo \"Error: no test specified\" && exit 1"
},
app.ts νμΌ μμ±
λ£¨νΈ ν¨μ€μ app.ts νμΌ μμ± ν μλμ κ°μ΄ μμ±
import express from 'express';
import cors from 'cors';
const port = 3000;
const app = express();
app.use(cors());
app.get('/', (req, res) => res.send('Hello, World!'));
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
REST URL κΈ°λ³Έ κ·μΉ
GET /products
GET /products/{id}
POST /products
PATCH /product/{id}
DELETE /product/{id}
μ€ν
npm run start
TO BE ADDED
How to set cors for other origin
How to set url path and divide api
Last updated