Notice
Recent Posts
Recent Comments
Link
개발일지
EXPRESS JS 기본 본문
보일러 플레이트
- 정보기술에서 말하는 보일러 플레이트는 변경 없이 계속하여 재 사용할 수 있는 저작품을 말한다
express
- npm install express --save
--sava
를 하면package.json
에 표시된다.- 참고사이트
- 위의 코드처럼
package.json
에"start": "node index.js"
를 작성하면 터미널에서npm run start
로 서버를 실행할 수 있다.
Mongo DB
npm install mongoose --save
const mongoose = require("mongoose"); mongoose .connect( "mongodb+srv://<아이디>:<비밀번호>@boilerplate-wi896.mongodb.net/test?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false, } ) .then(() => console.log("MongoDB Connected...")) .catch((err) => console.log(err));
위의 코드를 입력하면 몽고디비를 연결할 수 있다.
model, schema
// models/User.js // model의 schema 작성 const mongoose = require("mongoose"); const userSchema = mongoose.Schema({ name: { type: String, maxlength: 50, }, email: { type: String, trim: true, unique: 1, }, password: { type: String, minlength: 5, }, lastname: { type: String, maxlength: 5, }, role: { type: Number, default: 0, }, image: String, token: { type: String, }, tokenExp: { type: Number, }, }); const User = mongoose.model("User", userSchema); module.exports = { User };
Comments