Share
Sign In
📄

Node.js api server initial setting

Architecture setting
EXPRESS-API-TEMPLATE ├── node_modules ├── src │ ├── bin │ │ └── www.js | ├── database | | ├── query.js | | └── mysql.config.js │ ├── routes │ │ ├── api │ │ │ ├── index.js │ │ │ ├── some-project │ │ │ | ├── some-project.controller.js │ │ │ | └── index.js │ │ │ └── some-project2 │ │ │ ├── some-project2.controller.js │ │ │ └── index.js │ │ └── helper │ │ ├── aws │ │ │ └── index.js │ │ └── index.js │ └── app.js ├── .babelrc ├── .editorconfig ├── .eslintrc.json ├── .gitignore ├── .prettierrc ├── nodemon.json ├── package.json ├── README.md └── yarn.lock || package-lock.json
snippets
.editorconfig
다양한 IDE에서 동일한 프로젝트에서 작업하는 여러 개발자를 위해 일관된 코딩 스카일을 유지하는 데 도움이 되는 config
root = true [*] indent_style = space indent_size = 2 charset = utf-8 trim_trailing_whitespace = false insert_final_newline = true
.gitignore
private 파일이 깃에 푸시되지 않도록 하는 설정
코드
express-generator
1.
express-generator를 사용하여 가장 간단한 웹 서버를 생성한다.
2.
index/user.js 를 삭제한다.
3.
public/views/ 폴더를 삭제한다.
4.
bin/www에서 bin/www.js 로 변경한다.
5.
npm이나 yarn으로 jade를 삭제한다.
npm uninstall jade || yarn remove jade
6.
src/ 폴더를 생성하고 app.js 파일과 bin/routes/ 폴더를 src/ 폴더의 하위에 둔다.
7.
package.json에 “start” 키의 밸류를 아래와 같이 수정한다.
"start": "node ./src/bin/www"
8.
이제 다음에 나오는 설정을 차례차례 설정해준다.
ES6 setting
1.
src/app.js 를 아래와 같이 ES6 코드로 변경한다.
import logger from 'morgan'; import express from 'express'; import cookieParser from 'cookie-parser'; import indexRouter from './routes/index'; const app = express(); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(cookieParser()); app.use('/v1', indexRouter); export default app;
2.
src/bin/www.js 파일을 ES6로 변경해준다.
코드
3.
아래의 바벨 설정을 따라하며 바벨 세팅을 해준다. (바벨 설정을 안하면 제대로 동작하지 않을 수 있다.)
Babel, Nodemon, Eslint, Pretter setting
Babel setting
1.
babel install
npm
npm install @babel/cli @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/register @babel/runtime @babel/node --dev @babel/plugin-transform-async-to-generator @babel/plugin-transform-arrow-functions babel-plugin-module-resolver --dev
2.
루트 경로에 babelrc 폴더를 생성하고 아래의 코드를 추가시켜준다.
{ "presets": ["@babel/preset-env"], "plugins": [ ["@babel/plugin-transform-async-to-generator"], ["@babel/plugin-transform-arrow-functions"], ["@babel/transform-runtime"], [ "module-resolver", { "root": "./src", "alias": { "@": "./src" } } ] ] }
Nodemon setting
1.
nodemon install
# install nodemon yarn add nodemon --dev npm i nodemon
2.
루트 경로에 nodemon.json 폴더를 생성하고 아래의 코드를 입력한다.
{ "watch": [ "package.json", "nodemon.json", ".eslintrc.json", ".babelrc", ".prettierrc", "src/", ".env" ], "verbose": true, "ignore": ["*.test.js", "*.spec.js","node_modules"] }
3.
package.json에 script를 수정한다.
... "scripts": { "prestart": "babel ./src --out-dir build", "start": "nodemon ./build/bin/www", "dev": "nodemon --exec babel-node ./src/bin/www", }, ...
로컬에서 테스트를 위해 돌리는 상황이라면 npm run dev || yarn dev 를 수행하면 된다.
eslint prettier setting
1.
eslint와 prettier을 설치한다.
# install elsint and prettier yarn add eslint eslint-config-airbnb-base eslint-plugin-import prettier --dev npm install eslint eslint-config-airbnb-base eslint-plugin-import prettier --dev
2.
eslintrc.json 파일을 루트 경로에 생성한다.
{ "env": { "browser": true, "es6": true, "node": true, "mocha": true }, "extends": ["airbnb-base"], "globals": { "Atomics": "readonly", "SharedArrayBuffer": "readonly" }, "parserOptions": { "ecmaVersion": 2018, "sourceType": "module" }, "rules": { "indent": ["warn", 2], "linebreak-style": ["error", "unix"], "quotes": ["error", "single"], "semi": ["error", "always"], "no-console": 1, "comma-dangle": [0], "arrow-parens": [0], "object-curly-spacing": ["warn", "always"], "array-bracket-spacing": ["warn", "always"], "import/prefer-default-export": [0] } }
3.
루트 경로에 똑같이 .prettierrc 파일을 생성하고 아래의 코드를 입력한다.
{ "trailingComma": "es5", "tabWidth": 2, "semi": true, "singleQuote": true }
4.
package.json 의 script를 아래와 같이 수정한다.
... "scripts": { "prestart": "babel ./src --out-dir build", "start": "nodemon ./build/bin/www", "dev": "nodemon --exec babel-node ./src/bin/www", "lint": "./node_modules/.bin/eslint ./src", "pretty": "prettier --write '**/*.{js,json}' '!node_modules/**'", "postpretty": "npm run lint --fix" }, ...
.env Setting
1.
dotenv를 설치한다.
npm install dotenv yarn add dotenv
2.
src/ 폴더 밑에 setting.js 파일을 생성하고 아래의 코드를 집어 넣는다.
import dotenv from 'dotenv'; dotenv.config(); export const testEnvironmentVariable = process.env.TEST_ENV_VARIABLE;
사용 예제
Node.js 메뉴로 돌아가기
Node.js / Express.js
메인으로 돌아가기
Architecture setting
EXPRESS-API-TEMPLATE ├── node_modules ├── src │ ├── bin │ │ └── www.js | ├── database | | ├── query.js | | └── mysql.config.js │ ├── routes │ │ ├── api │ │ │ ├── index.js │ │ │ ├── some-project │ │ │ | ├── some-project.controller.js │ │ │ | └── index.js │ │ │ └── some-project2 │ │ │ ├── some-project2.controller.js │ │ │ └── index.js │ │ └── helper │ │ ├── aws │ │ │ └── index.js │ │ └── index.js │ └── app.js ├── .babelrc ├── .editorconfig ├── .eslintrc.json ├── .gitignore ├── .prettierrc ├── nodemon.json ├── package.json ├── README.md └── yarn.lock || package-lock.json
snippets
.editorconfig
다양한 IDE에서 동일한 프로젝트에서 작업하는 여러 개발자를 위해 일관된 코딩 스카일을 유지하는 데 도움이 되는 config
root = true [*] indent_style = space indent_size = 2 charset = utf-8 trim_trailing_whitespace = false insert_final_newline = true
.gitignore
private 파일이 깃에 푸시되지 않도록 하는 설정
코드
express-generator
1.
express-generator를 사용하여 가장 간단한 웹 서버를 생성한다.
2.
index/user.js 를 삭제한다.
3.
public/views/ 폴더를 삭제한다.
4.
bin/www에서 bin/www.js 로 변경한다.
5.
npm이나 yarn으로 jade를 삭제한다.
npm uninstall jade || yarn remove jade
6.
src/ 폴더를 생성하고 app.js 파일과 bin/routes/ 폴더를 src/ 폴더의 하위에 둔다.
7.
package.json에 “start” 키의 밸류를 아래와 같이 수정한다.
"start": "node ./src/bin/www"
8.
이제 다음에 나오는 설정을 차례차례 설정해준다.
ES6 setting
1.
src/app.js 를 아래와 같이 ES6 코드로 변경한다.
import logger from 'morgan'; import express from 'express'; import cookieParser from 'cookie-parser'; import indexRouter from './routes/index'; const app = express(); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(cookieParser()); app.use('/v1', indexRouter); export default app;
2.
src/bin/www.js 파일을 ES6로 변경해준다.
코드
3.
아래의 바벨 설정을 따라하며 바벨 세팅을 해준다. (바벨 설정을 안하면 제대로 동작하지 않을 수 있다.)
Babel, Nodemon, Eslint, Pretter setting
Babel setting
1.
babel install
npm
npm install @babel/cli @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/register @babel/runtime @babel/node --dev @babel/plugin-transform-async-to-generator @babel/plugin-transform-arrow-functions babel-plugin-module-resolver --dev
2.
루트 경로에 babelrc 폴더를 생성하고 아래의 코드를 추가시켜준다.
{ "presets": ["@babel/preset-env"], "plugins": [ ["@babel/plugin-transform-async-to-generator"], ["@babel/plugin-transform-arrow-functions"], ["@babel/transform-runtime"], [ "module-resolver", { "root": "./src", "alias": { "@": "./src" } } ] ] }
Nodemon setting
1.
nodemon install
# install nodemon yarn add nodemon --dev npm i nodemon
2.
루트 경로에 nodemon.json 폴더를 생성하고 아래의 코드를 입력한다.
{ "watch": [ "package.json", "nodemon.json", ".eslintrc.json", ".babelrc", ".prettierrc", "src/", ".env" ], "verbose": true, "ignore": ["*.test.js", "*.spec.js","node_modules"] }
3.
package.json에 script를 수정한다.
... "scripts": { "prestart": "babel ./src --out-dir build", "start": "nodemon ./build/bin/www", "dev": "nodemon --exec babel-node ./src/bin/www", }, ...
로컬에서 테스트를 위해 돌리는 상황이라면 npm run dev || yarn dev 를 수행하면 된다.
eslint prettier setting
1.
eslint와 prettier을 설치한다.
# install elsint and prettier yarn add eslint eslint-config-airbnb-base eslint-plugin-import prettier --dev npm install eslint eslint-config-airbnb-base eslint-plugin-import prettier --dev
2.
eslintrc.json 파일을 루트 경로에 생성한다.
{ "env": { "browser": true, "es6": true, "node": true, "mocha": true }, "extends": ["airbnb-base"], "globals": { "Atomics": "readonly", "SharedArrayBuffer": "readonly" }, "parserOptions": { "ecmaVersion": 2018, "sourceType": "module" }, "rules": { "indent": ["warn", 2], "linebreak-style": ["error", "unix"], "quotes": ["error", "single"], "semi": ["error", "always"], "no-console": 1, "comma-dangle": [0], "arrow-parens": [0], "object-curly-spacing": ["warn", "always"], "array-bracket-spacing": ["warn", "always"], "import/prefer-default-export": [0] } }
3.
루트 경로에 똑같이 .prettierrc 파일을 생성하고 아래의 코드를 입력한다.
{ "trailingComma": "es5", "tabWidth": 2, "semi": true, "singleQuote": true }
4.
package.json 의 script를 아래와 같이 수정한다.
... "scripts": { "prestart": "babel ./src --out-dir build", "start": "nodemon ./build/bin/www", "dev": "nodemon --exec babel-node ./src/bin/www", "lint": "./node_modules/.bin/eslint ./src", "pretty": "prettier --write '**/*.{js,json}' '!node_modules/**'", "postpretty": "npm run lint --fix" }, ...
.env Setting
1.
dotenv를 설치한다.
npm install dotenv yarn add dotenv
2.
src/ 폴더 밑에 setting.js 파일을 생성하고 아래의 코드를 집어 넣는다.
import dotenv from 'dotenv'; dotenv.config(); export const testEnvironmentVariable = process.env.TEST_ENV_VARIABLE;
사용 예제
Node.js 메뉴로 돌아가기
Node.js / Express.js
메인으로 돌아가기
Architecture setting
EXPRESS-API-TEMPLATE ├── node_modules ├── src │ ├── bin │ │ └── www.js | ├── database | | ├── query.js | | └── mysql.config.js │ ├── routes │ │ ├── api │ │ │ ├── index.js │ │ │ ├── some-project │ │ │ | ├── some-project.controller.js │ │ │ | └── index.js │ │ │ └── some-project2 │ │ │ ├── some-project2.controller.js │ │ │ └── index.js │ │ └── helper │ │ ├── aws │ │ │ └── index.js │ │ └── index.js │ └── app.js ├── .babelrc ├── .editorconfig ├── .eslintrc.json ├── .gitignore ├── .prettierrc ├── nodemon.json ├── package.json ├── README.md └── yarn.lock || package-lock.json
snippets
.editorconfig
다양한 IDE에서 동일한 프로젝트에서 작업하는 여러 개발자를 위해 일관된 코딩 스카일을 유지하는 데 도움이 되는 config
root = true [*] indent_style = space indent_size = 2 charset = utf-8 trim_trailing_whitespace = false insert_final_newline = true
.gitignore
private 파일이 깃에 푸시되지 않도록 하는 설정
코드
express-generator
1.
express-generator를 사용하여 가장 간단한 웹 서버를 생성한다.
2.
index/user.js 를 삭제한다.
3.
public/views/ 폴더를 삭제한다.
4.
bin/www에서 bin/www.js 로 변경한다.
5.
npm이나 yarn으로 jade를 삭제한다.
npm uninstall jade || yarn remove jade
6.
src/ 폴더를 생성하고 app.js 파일과 bin/routes/ 폴더를 src/ 폴더의 하위에 둔다.
7.
package.json에 “start” 키의 밸류를 아래와 같이 수정한다.
"start": "node ./src/bin/www"
8.
이제 다음에 나오는 설정을 차례차례 설정해준다.
ES6 setting
1.
src/app.js 를 아래와 같이 ES6 코드로 변경한다.
import logger from 'morgan'; import express from 'express'; import cookieParser from 'cookie-parser'; import indexRouter from './routes/index'; const app = express(); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(cookieParser()); app.use('/v1', indexRouter); export default app;
2.
src/bin/www.js 파일을 ES6로 변경해준다.
코드
3.
아래의 바벨 설정을 따라하며 바벨 세팅을 해준다. (바벨 설정을 안하면 제대로 동작하지 않을 수 있다.)
Babel, Nodemon, Eslint, Pretter setting
Babel setting
1.
babel install
npm
npm install @babel/cli @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/register @babel/runtime @babel/node --dev @babel/plugin-transform-async-to-generator @babel/plugin-transform-arrow-functions babel-plugin-module-resolver --dev
2.
루트 경로에 babelrc 폴더를 생성하고 아래의 코드를 추가시켜준다.
{ "presets": ["@babel/preset-env"], "plugins": [ ["@babel/plugin-transform-async-to-generator"], ["@babel/plugin-transform-arrow-functions"], ["@babel/transform-runtime"], [ "module-resolver", { "root": "./src", "alias": { "@": "./src" } } ] ] }
Nodemon setting
1.
nodemon install
# install nodemon yarn add nodemon --dev npm i nodemon
2.
루트 경로에 nodemon.json 폴더를 생성하고 아래의 코드를 입력한다.
{ "watch": [ "package.json", "nodemon.json", ".eslintrc.json", ".babelrc", ".prettierrc", "src/", ".env" ], "verbose": true, "ignore": ["*.test.js", "*.spec.js","node_modules"] }
3.
package.json에 script를 수정한다.
... "scripts": { "prestart": "babel ./src --out-dir build", "start": "nodemon ./build/bin/www", "dev": "nodemon --exec babel-node ./src/bin/www", }, ...