forked from livingalone-2023/livingalone-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
2,293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const express = require('express'); | ||
const session = require('express-session'); | ||
const cors = require('cors'); | ||
const multer = require('multer'); | ||
const sequelize = require("./config/config"); | ||
|
||
const path = require('path'); | ||
const bodyParse = require('body-parser') | ||
|
||
const app = express(); | ||
const port = 3000; | ||
|
||
// routes | ||
const userRouter = require('./routes/user') | ||
|
||
// app.get('/', (req, res) => { | ||
// return res.send("홀로서기"); | ||
// }) | ||
|
||
sequelize.sync() | ||
.then(() => { | ||
console.log('Database synced') | ||
}) | ||
.catch((err) => { | ||
console.error('Error syncing database:', err) | ||
}); | ||
|
||
// 미들웨어 | ||
app.use(cors()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(require('cookie-parser')()); | ||
app.use(bodyParse.json()) | ||
|
||
// router - 진입할 엔드포인트 + 진입할 라우터 | ||
app.use('/users', userRouter) | ||
|
||
app.listen(port, () => { | ||
console.log(port, '번 포트에서 대기 중'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// db랑 시퀄라이즈랑 연결하는 역할 | ||
const Sequelize = require('sequelize'); | ||
require('dotenv').config(); | ||
|
||
const sequelize = new Sequelize(process.env.MYSQL_DATABASE, process.env.MYSQL_USERNAME, process.env.MYSQL_PASSWORD, { | ||
host: process.env.MYSQL_HOST, | ||
port: process.env.MYSQL_PORT, | ||
dialect: 'mysql', | ||
}) | ||
|
||
// 연결 테스트 | ||
sequelize | ||
.authenticate() | ||
.then(() => { | ||
console.log('Connected to the config'); | ||
}) | ||
.catch((err) => { | ||
console.error('Unable to connect to the config:', err); | ||
}); | ||
|
||
module.exports = sequelize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// module export | ||
// const로 상수 선언 | ||
const sequelize = require('../config/config') | ||
|
||
const User = require('./user')(sequelize);// sequelize가 db 정보를 가지고 있으므로 전달해주면 얘가 만들어봄 | ||
|
||
module.exports = { | ||
User, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// models/User.js | ||
const { DataTypes } = require('sequelize'); | ||
|
||
const User = (sequelize) => sequelize.define('users', { | ||
id: { | ||
type: DataTypes.BIGINT, | ||
primaryKey: true, | ||
allowNull: false, | ||
autoIncrement: true, | ||
unique: true, | ||
}, | ||
user_id: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: true, | ||
}, | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: true, | ||
}, | ||
password: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
email: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: true, | ||
validate: { | ||
isEmail: true, | ||
}, | ||
} | ||
}); | ||
|
||
module.exports = User; |
Oops, something went wrong.