정민우

[New] Backend initial commit

PORT=3000
MYSQL_USERNAME=root
MYSQL_PASSWORD=
MYSQL_DATABASE=root
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
CORS_ALLOWED_URL=
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
.env
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.idea
npm-debug.log*
yarn-debug.log*
yarn-error.log*
let express = require('express')
let cookieParser = require('cookie-parser')
var cors = require('cors')
var corsConfig = require('./config/cors')
let indexRouter = require('./routes/index')
let app = express()
app.use(cors(corsConfig))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use('/', indexRouter)
app.use(function(req, res) {
res.status(400)
res.json({
success: false,
message: 'error'
})
})
app.use(function(err, req, res) {
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
res.status(err.status || 500)
res.json({
success: false
})
})
module.exports = app
let app = require('../app')
let debug = require('debug')('sw-fest-api:server')
let http = require('http')
let port = normalizePort(process.env.PORT || '3000')
app.set('port', port)
let server = http.createServer(app)
server.listen(port)
server.on('error', onError)
server.on('listening', onListening)
function normalizePort(val) {
let port = parseInt(val, 10)
if (isNaN(port)) {
return val
}
if (port >= 0) {
return port
}
return false
}
function onError(error) {
if (error.syscall !== 'listen') {
throw error
}
let bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges')
process.exitCode = 1
break
case 'EADDRINUSE':
console.error(bind + ' is already in use')
process.exitCode = 1
break
default:
throw error
}
}
function onListening() {
let addr = server.address()
let bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port
debug('Listening on ' + bind)
}
File mode changed
const path = require('path')
require('dotenv').config({path: path.join(__dirname, "../.env")})
const username = process.env.MYSQL_USERNAME || 'root'
const password = process.env.MYSQL_PASSWORD || null
const database = process.env.MYSQL_DATABASE || 'root'
const host = process.env.MYSQL_HOST || '127.0.0.1'
const port = process.env.MYSQL_PORT || 3306
module.exports = {
'username': username,
'password': password,
'database': database,
'host': host,
'port': port,
'dialect': 'mysql',
'operatorsAliases': false
}
module.exports = {
origin: [
'http://localhost:8560',
...(process.env.CORS_ALLOWED_URL ? process.env.CORS_ALLOWED_URL.split(',') : []),
],
credentials: true,
optionsSuccessStatus: 200
}
File mode changed
'use strict'
const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const basename = path.basename(__filename)
const config = require(__dirname + '/../config/config')
const db = {}
let sequelize
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config)
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config)
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file))
db[model.name] = model
})
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db)
}
})
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
This diff is collapsed. Click to expand it.
{
"name": "sw-fest-api",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "sequelize db:migrate && node ./bin/www --env-update"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^8.2.0",
"express": "~4.16.1",
"mysql2": "^2.1.0",
"sequelize": "^6.3.5"
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-plugin-node": "^11.1.0",
"sequelize-cli": "^6.2.0"
}
}
### Docker VPS-Service
## OSS-2021
File mode changed
let express = require('express')
let router = express.Router()
router.get('/', function(req, res) {
res.json({message: 'hi'})
})
module.exports = router
File mode changed