-
MongoDB, Mongoose and NoSQLJavascript/Node.js and Express 2021. 7. 15. 17:38
App.js
const mongoose = require("mongoose"); //User라는 스키마불러오기 const User = require("../models/user"); //저장할 db위치, 이름, 접근 등 설정 mongoose.connect("mongodb://localhost:27017/admin", { useNewUrlParser: true, useUnifiedTopology: true, user: 'test', pass: 'test', }); mongoose.set("useFindAndModify", false); mongoose.set("useCreateIndex", true); //연결에러시 콘솔에 에러 띄우기 const db = mongoose.connection; db.on("error", console.error.bind(console, "connection error:")); //User스키마를 이용해 회원가입후 db에 회원정보 저장 router.post("/register", async (req, res) => { //값 받아오기 const { username, password } = req.body; //디비에 들어갈 내용들 const user = new User({ username, password }); //디비에 저장시도 await user.save(); //결과 알려주기 res.status(201).send({ result: '회원가입이 완료되었습니다' }); })
Schema(model)
const mongoose = require('mongoose'); const UserSchema = new mongoose.Schema({ username: { type: String, required: true, }, password: { type: String, required: true, }, }) module.exports = mongoose.model('User', UserSchema);
'Javascript > Node.js and Express' 카테고리의 다른 글
imageUpload and Multer(Javascript, Node.js) (0) 2021.07.15 EC2, S3 and CORS (0) 2021.07.15 Node.JS, Express and Javascript (0) 2021.07.15 Sequelize and SQL (0) 2021.07.15 Python and Flask (0) 2021.07.15