파일 구조
index.js
- expree 연결, db 연결, api router 연결
models/index.js
- 시퀄라이즈(mysql) 연결, config 파일(db정보)연결, 각 모델들을 db에 담아 db.User 등... 으로 사용할 수 있게 설정
routes/post.js
routes/posts.js
routes/user.js
index.js
const express = require('express');
const db = require('./models');
const userAPIRouter = require('./routes/user');
const postAPIRouter = require('./routes/post');
const postsAPIRouter = require('./routes/posts');
const app = express();
db.sequelize.sync();
// 다른 서비스가 내 서비스의 기능을 실행할 수 있게 열어둔 창구
app.use('/api/user', userAPIRouter);
app.use('/api/post', postAPIRouter);
app.use('/api/posts', postsAPIRouter);
app.listen(8080, () => {
console.log('hello server ! http://localhost:8080');
});
models/index.js
const Sequelize = require('sequelize');
const env = process.env.NODE_ENV || 'development';
const config = require('../config/config')[env];
const db = {};
const sequelize = new Sequelize(config.database, config.username, config.password, config);
db.Comment = require('./comment')(sequelize, Sequelize);
db.User = require('./user')(sequelize, Sequelize);
db.Hashtag = require('./hashtag')(sequelize, Sequelize);
db.Image = require('./image')(sequelize, Sequelize);
db.Post = require('./post')(sequelize, Sequelize);
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
routes/user.js
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
});
router.post('/', (req, res) => {
});
router.get('/:id', (req, res) => {
});
router.post('/logout', (req, res) => {
});
router.post('/login', (req, res) => {
});
router.get('/:id/follow', (req, res) => {
});
router.post('/:id/follow', (req, res) => {
});
router.delete('/:id/follower', (req, res) => {
});
router.get('/:id/posts', (req, res) => {
});
module.exports = router;