https://yeon960.tistory.com/270
nodejs로 SNS 만들기 #1
1. 프로젝트 세팅 시작 1. package.json 생성하기 $npm init 2. package.json 작성하기 { "name": "nodeinsta", "version": "1.0.0", "description": "노드 인스타", "main": "app.js", "scripts": { "start": "no..
yeon960.tistory.com
이전편(프로젝트 세팅)은 요기👆
데이터베이스 세팅하기
1. 폴더 생성
models 폴더 안 user.js, post.js, hashtag.js를 생성
1) user.js
// models/user.js
module.exports = (sequelize, DataTypes) => (
sequelize.define('user', {
email: {
type: DataTypes.STRING(40),
allowNull: true,
unique: true,
},
nick: {
type: DataTypes.STRING(15),
allowNull: false,
},
password: {
type: DataTypes.STRING(100),
allowNull: true,
},
provider: {
type: DataTypes.STRING(10),
allowNull: false,
defaultValue: 'local',
},
snsId: {
type: DataTypes.STRING(30),
allowNull: true,
},
}, {
timestamps: true,
paranoid: true,
})
);
사용자 정보를 저장하는 모델.
이메일, 닉네임, 비밀번호 저장.
sns 로그인 - provider와 snsId 저장
provider가 kakao면 카카오 로그인
provider가 local이면 로컬 로그인(기본 로그인).
2) post.js
내용과 이미지 경로 저장.
게시글 등록자의 아이디를 담은 컬럼은 나중에 시퀄라이즈가 알아서 생성해준다.
// models/post.js
module.exports = (sequelize, DataTypes) => (
sequelize.define('post', {
content: {
type: DataTypes.STRING(140),
allowNull: false,
},
img: {
type: DataTypes.STRING(200),
allowNull: true,
},
}, {
timestamps: true,
paranoid: true,
})
);
3) hashtag.js
// models/hashtag.js
module.exports = (sequelize, DataTypes) => (
sequelize.define('hashtag', {
title: {
type: DataTypes.STRING(15),
allowNull: false,
unique: true,
},
}, {
timestamps: true,
paranoid: true,
})
);
해시태그 모델은 태그 이름을 저장.
왜 저장해? 나중에 태그로 검색하기 위함.
2. 생성한 모델들 시퀄라이즈 등록
// 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.sequelize = sequelize;
db.Sequelize = Sequelize;
db.User = require('./user')(sequelize, Sequelize);
db.Post = require('./post')(sequelize, Sequelize);
db.Hashtag = require('./hashtag')(sequelize, Sequelize);
db.User.hasMany(db.Post);
db.Post.belongsTo(db.User);
db.Post.belongsToMany(db.Hashtag, { through: 'PostHashtag' });
db.Hashtag.belongsToMany(db.Post, { through: 'PostHashtag' });
db.User.belongsToMany(db.User, {
foreignKey: 'followingId',
as: 'Followers',
through: 'Follow',
});
db.User.belongsToMany(db.User, {
foreignKey: 'followerId',
as: 'Followings',
through: 'Follow',
});
module.exports = db;
통째로 바꿔준다.
3. config.json 수정
config/config.json 파일 수정.
development의 password부분을 본인 MySQL 비밀번호를 넣어주고, 데이터베이스 이름도 nodeInsta로 바꿔준다.
$sequelize db:create
명령어 입력하면 데이터베이스가 생성된다.
4. 모델을 서버와 연결
//app.js
...
const pageRouter = require('./routes/page'); // 라우터 가져오기
const { sequelize } = require('./models'); // 추가1-모델 서버 연결
const app = express();
sequelize.sync(); // 추가2-모델 서버 연결
app.set('views', path.join(__dirname, 'views'));
...
서버 세팅 끝~~
5. 서버 실행
$npm start
데이터베이스 세팅 완료되었다.
이제 사용자 정보를 저장할 수 있다.
다음편(로그인 구현)은 요기👇
https://yeon960.tistory.com/272
nodejs로 SNS 만들기 #3
https://yeon960.tistory.com/271 nodejs로 SNS 만들기 #2 https://yeon960.tistory.com/270 nodejs로 SNS 만들기 #1 1. 프로젝트 세팅 시작 1. package.json 생성하기 $npm init 2. package.json 작성하기 { "nam..
yeon960.tistory.com
'개인공부 > Node.js' 카테고리의 다른 글
nodejs로 SNS 만들기 #4 (1) | 2022.08.15 |
---|---|
User.find is not a function 오류 발생 시 해결 방법 (0) | 2022.08.15 |
nodejs로 SNS 만들기 #3 (0) | 2022.08.15 |
nodejs로 SNS 만들기 #1 (0) | 2022.08.09 |
[터미널 설정 문제] 'node' 용어가 cmdlet, 함수, 스크립트 파일 또는 실행할 수 있는 프로그램 이름으로 인식되지 않습니다. (0) | 2022.07.21 |