Script/Node.js

[Node.js] Node.js 유용한 패키지들

Bogass 2025. 1. 13. 14:34

http-status-codes

  • status code 를 더욱 가독성 좋게 쓸 수 있도록 하는 패키지
    • 200 : StatusCodes.OK
    • 201 : StatusCodes.CREATED
    • 404 : StatusCodes.NOT_FOUND
    • 400 : StatusCodes.BAD_REQUEST
    • 500 : StatusCodes.INTERNAL_SERVER_ERROR
npm install http-status-codes
const { StatusCodes } = require('http-status-codes');

// ...
catch(err){
    //res.status(404).json({error:err.message});
    res.status(StatusCodes.NOT_FOUND).json({error:err.message}); // 404
}

 

 

http-status-codes

Constants enumerating the HTTP status codes. Based on the Java Apache HttpStatus API.. Latest version: 2.3.0, last published: a year ago. Start using http-status-codes in your project by running `npm i http-status-codes`. There are 2832 other projects in t

www.npmjs.com

 

serve-favicon

  • server단에서 favicon을 적용 해주는 패키지
npm install serve-favicon
// app.js
var favicon = require('serve-favicon');
app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon', 'favicon.ico')));

 

 

serve-favicon

favicon serving middleware with caching. Latest version: 2.5.0, last published: 7 years ago. Start using serve-favicon in your project by running `npm i serve-favicon`. There are 2729 other projects in the npm registry using serve-favicon.

www.npmjs.com

 

express-generator

프로젝트 셋팅을 위한 패키지

npm install -g express-generator
express [projectname]

 

 

express-generator

Express' application generator. Latest version: 4.16.1, last published: 6 years ago. Start using express-generator in your project by running `npm i express-generator`. There are 35 other projects in the npm registry using express-generator.

www.npmjs.com

 

body-parser

  • json데이터 요청 본문을 파싱해주는 패키지
npm install body-parser
app.use(require('body-parser').json());

 

 

body-parser

Node.js body parsing middleware. Latest version: 1.20.3, last published: 4 months ago. Start using body-parser in your project by running `npm i body-parser`. There are 26313 other projects in the npm registry using body-parser.

www.npmjs.com

 

mongoose

  • mongoDB와 nodejs 연동을 위한 패키지
npm install mongoose
// mongoose 연동
const mongoose = require('mongoose');
// mongodb atlas -> Application Development → Get conntection string에서 확인한 연동 url
const uri = "mongodb+srv://~~";
const clientOptions = { serverApi: { version: '1', strict: true, deprecationErrors: true } };
async function run() {
  try {
    // Create a Mongoose client with a MongoClientOptions object to set the Stable API version
    await mongoose.connect(uri, clientOptions);
    await mongoose.connection.db.admin().command({ ping: 1 });
    console.log("Pinged your deployment. You successfully connected to MongoDB!");
  } finally {
    // Ensures that the client will close when you finish/error
    // await mongoose.disconnect();
  }
}
run().catch(console.dir);

 

 

mongoose

Mongoose MongoDB ODM. Latest version: 8.9.4, last published: 3 days ago. Start using mongoose in your project by running `npm i mongoose`. There are 19468 other projects in the npm registry using mongoose.

www.npmjs.com

 

dotenv

  • 환경변수를 .env파일로 관리해주는 패키지
# .env
URL=http://localhost:3000
// app.js
const uri = process.env.MONGO_URI;

 

 

dotenv

Loads environment variables from .env file. Latest version: 16.4.7, last published: a month ago. Start using dotenv in your project by running `npm i dotenv`. There are 51862 other projects in the npm registry using dotenv.

www.npmjs.com

 

EJS

  • 뷰엔진을 적용하고 레이아웃을 관리하는 패키지
npm install ejs # ejs 템플릿엔진
npm install express-ejs-layouts # ejs 레이아웃 라이브러리
// app.js
const expressLayouts = require('express-ejs-layouts');

app.set("view engine", "ejs"); // 뷰엔진 ejs로 설정
app.use(expressLayouts); // 레이아웃 미들웨어 추가
app.set('layout', 'layout/layout'); // 레이아웃파일 설정
app.set('layout extractScripts', true); // 스크립트코드 설정

 

 

ejs

Embedded JavaScript templates. Latest version: 3.1.10, last published: 9 months ago. Start using ejs in your project by running `npm i ejs`. There are 14483 other projects in the npm registry using ejs.

www.npmjs.com

 

Axios

  • HTTP 요청을 보내기 위해 사용
  • 비동기 방식 HTTP 통신을 가능하게 해주며 리턴을 promise객체로 함.
npm install axios
axios({
  method: 'get',
  url: '/path',
  data: {
    name: 'hy',
    age: 28
  }
});

 

 

Cookie-Session

  • cookie-session: 세션 관리를 위해 사용.
npm install cookie-session
// 세션 설정
app.use(cookieSession({
  name: 'google-auth-session',
  keys: ['your-secret-key'],
  maxAge: 24 * 60 * 60 * 1000 // 세션 만료 시간 24시간
}));

 

 

cookie-session

cookie session middleware. Latest version: 2.1.0, last published: a year ago. Start using cookie-session in your project by running `npm i cookie-session`. There are 10503 other projects in the npm registry using cookie-session.

www.npmjs.com