Skip to content

Commit

Permalink
[#4] HTTP API 기본 CRUD 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Winters0727 authored and dlckdduq1107 committed Sep 23, 2021
1 parent fd6895c commit 94fc54c
Show file tree
Hide file tree
Showing 10 changed files with 1,078 additions and 42 deletions.
23 changes: 8 additions & 15 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
const apiRouter = require('./routes/api');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
const app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/api', apiRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down
30 changes: 21 additions & 9 deletions backend/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('backend:server');
var http = require('http');
const app = require('../app');
const debug = require('debug')('backend:server');
const http = require('http');

require('dotenv').config();
const mongoose = require('mongoose');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);
const server = http.createServer(app);

/**
* Connect Mongo DB.
*/

mongoose.connect(process.env.DATABASE_URL)
.catch((err) => {
console.error(err);
});

/**
* Listen on provided port, on all network interfaces.
Expand All @@ -34,7 +46,7 @@ server.on('listening', onListening);
*/

function normalizePort(val) {
var port = parseInt(val, 10);
const port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
Expand All @@ -58,7 +70,7 @@ function onError(error) {
throw error;
}

var bind = typeof port === 'string'
const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

Expand All @@ -82,8 +94,8 @@ function onError(error) {
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
Expand Down
68 changes: 68 additions & 0 deletions backend/controllers/video.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const Video = require('../models/video')

const postVideo = async (req, res, next) => {
try {
await Video.create({...req.body});
await res.status(201).json({
'message' : 'success'
});
}

catch(err) {
await res.status(500).json({
'error' : err
});
}
};

const getVideo = async (req, res, next) => {
try {
const videoId = req.params['videoId'];
const video = await Video.findById(videoId);
await res.status(200).json({
'message' : 'success',
'Video': video
});
}

catch(err) {
await res.status(500).json({
'error' : err
});
}

};

const updateVideo = async (req, res, next) => {
try {
const videoId = req.params['videoId'];
await Video.findByIdAndUpdate(videoId, {...req.body, 'updatedAt' : Date.now()});
await res.status(200).json({
'message' : 'success'
});
}

catch(err) {
await res.status(500).json({
'error' : err
});
}
};

const deleteVideo = async (req, res, next) => {
try {
const videoId = req.params['videoId'];
await Video.findByIdAndDelete(videoId);
await res.status(200).json({
'message' : 'success'
});
}

catch(err) {
await res.status(500).json({
'error' : err
});
}
};

module.exports = { postVideo, getVideo, updateVideo, deleteVideo };
15 changes: 15 additions & 0 deletions backend/models/video.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema

const videoSchema = new Schema({
location : {
type : String,
required : true
},
videoURL : {
type : String,
required : true
}
})

module.exports = mongoose.model('Video', videoSchema)
Loading

0 comments on commit 94fc54c

Please sign in to comment.