Commit 8385a937 authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

server 와 user 수정

parent 44f8debf
{
"name": "quiz-competition",
"version": "1.0.0",
"main": "index.js",
"repository": "https://compmath.korea.ac.kr/gitlab/research/quiz-competition.git",
"author": "Yoon, Daeki <dowellware@gmail.com>",
"license": "MIT",
"type": "module",
"scripts": {
"dev": "nodemon src/server/server.js"
},
"dependencies": {
"bcrypt": "^5.0.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"express-jwt": "^6.0.0",
"jsonwebtoken": "^8.5.1",
"mongodb": "^3.6.2",
"mongoose": "^5.10.7",
"nodemon": "^2.0.4"
}
}
import jwt from 'jsonwebtoken'
const signin = async (req, res) => {
try {
let user
} catch (error) {
return res.status(400).json({
error: 'User not found'
})
}
}
export default {
signin,
}
\ No newline at end of file
const config = {
env: process.env.NODE_ENV || 'development',
port: process.env.PORT || 3000,
mongoUri: process.env.MONGODB_URI || 'mongodb://localhost:27017/quizcompetition'
}
export default config
\ No newline at end of file
import express from 'express'
import bodyParser from 'body-parser'
import userRoutes from './user/user.routes.js'
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use('/', userRoutes)
app.use((err, req, res, next) => {
if (err) {
console.log('Error in Express', err)
}
})
export default app
\ No newline at end of file
import mongoose from 'mongoose'
import app from './express.js'
import config from './config/config.js';
mongoose.Promise = global.Promise
mongoose.connect(config.mongoUri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
})
mongoose.connection.on('error', () => {
throw new Error(`unable to connect to database: ${config.mongoUri}`)
})
app.listen(config.port, () => {
console.info('Server started on port %s.', config.port)
})
\ No newline at end of file
import User from './user.model.js';
const create = async (req, res) => {
const user = new User(req.body)
try {
await user.save()
return res.json({
message: 'Succefully signed up!'
})
} catch (error) {
return res.status(400).json({
error: 'User creation error'
})
}
}
const list = async (req, res) => {
try {
let users = await User.find().select('name email updated created').exec()
return res.json(users)
} catch (error) {
return res.status(400).json({
error: 'User not found'
})
}
}
export default {
create,
list,
}
\ No newline at end of file
import mongoose from 'mongoose'
import bcrypt from 'bcrypt'
const UserSchema = new mongoose.Schema({
name: {
type: String,
trim: true,
required: 'Name is required'
},
email: {
type: String,
trim: true,
unique: 'Email is already exists',
match: [/.+\@.+\..+/, 'Please fill a valid email address'],
required: 'Email is required'
},
created: {
type: Date,
default: Date.now,
},
updated: Date,
hashedPassword: {
type: String,
required: 'Password is required'
},
salt: String,
})
UserSchema.virtual('password')
.set(function (password) {
this._password = password
this.salt = bcrypt.genSaltSync()
this.hashedPassword = bcrypt.hashSync(password, this.salt)
})
.get(() => {
return this._password
})
UserSchema.methods.authenticate = function (plainText) {
return bcrypt.compareSync(plainText, this.hashedPassword)
}
UserSchema.path('hashedPassword').validate(function (value) {
if (this._password && this._password.length < 6) {
this.invalidate('password', 'Password must be at least 6 characters.')
}
if (this.isNew && !this._password) {
this.invalidate('password', 'Password is required')
}
})
export default mongoose.model('User', UserSchema)
\ No newline at end of file
import express from 'express'
import userCtrl from './user.controller.js'
const router = express.Router()
router.route('/api/users')
.get(userCtrl.list)
.post(userCtrl.create)
export default router
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment