Sequelize
Sequelize is a modern TypeScript and Node.js ORM for Oracle, Postgres, MySQL, MariaDB, SQLite and SQL Server, and more. Featuring solid transaction support, relations, eager and lazy loading, read replication and more.
sequelize.org
1. 라이브러리 설치
npm i sequelize
npm i mysql2
2. DB 연결 (app.js)
const express = require('express');
const app = express();
const port = 3000;
const { Sequelize } = require('sequelize');
// database, username, password는 자신의 DB에 맞는 값을 입력해야 한다.
// username: root (주로)
// password : 설정하지 않았으면 null 과 '' 중에 하나 입력
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'mysql'
});
app.get('/', (req, res) => {
res.send('Hello World!');
})
app.listen(port, async () => {
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
console.log(`서버가 실행됩니다. http://localhost:${port}`);
})
'프로그래밍 > Node.js' 카테고리의 다른 글
JWT 기본 개념/인증,인가 (0) | 2023.11.07 |
---|---|
[Sequelize] DB연결 에러 (0) | 2023.08.07 |
REST API 설계하여 요청 및 응답하기 : body-parser 이용 (0) | 2023.07.29 |
REST API : 경로 Naming, HTTP Method (0) | 2023.07.26 |
응답방법/요청 데이터 사용법 (0) | 2023.07.25 |