init repo
commit
1a47edd0dc
@ -0,0 +1 @@
|
||||
node_modules/
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "express-api-starter",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node src/index.js",
|
||||
"dev": "nodemon src/index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^8.2.0",
|
||||
"express": "^4.17.1",
|
||||
"helmet": "^4.1.0",
|
||||
"morgan": "^1.10.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.4"
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
NODE_ENV=development
|
||||
PORT=5000
|
@ -0,0 +1,11 @@
|
||||
const express = require('express')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
res.json({
|
||||
message: 'API!'
|
||||
})
|
||||
})
|
||||
|
||||
module.exports = router
|
@ -0,0 +1,27 @@
|
||||
const express = require('express')
|
||||
const morgan = require('morgan')
|
||||
const helmet = require('helmet')
|
||||
const cors = require('cors')
|
||||
|
||||
require('dotenv').config()
|
||||
|
||||
const middlewares = require('./middlewares')
|
||||
const api = require('./api')
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use(morgan('dev'))
|
||||
app.use(helmet())
|
||||
app.use(cors())
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.json({
|
||||
message: 'Hello!'
|
||||
})
|
||||
})
|
||||
|
||||
app.use('/api/v1', api)
|
||||
app.use(middlewares.notFound)
|
||||
app.use(middlewares.errorHandler)
|
||||
|
||||
module.exports = app
|
@ -0,0 +1,7 @@
|
||||
const app = require('./app')
|
||||
|
||||
const port = process.env.PORT || 5000;
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Listening: http://localhost:${port}`)
|
||||
})
|
@ -0,0 +1,19 @@
|
||||
function notFound(req, res, next) {
|
||||
res.status(404);
|
||||
const error = new Error(`Not Found - ${req.originalUrl}`)
|
||||
next(error)
|
||||
}
|
||||
|
||||
function errorHandler(err, req, res, next) {
|
||||
const statusCode = res.statusCode !== 200 ? res.statusCode : 500
|
||||
res.status(statusCode)
|
||||
res.json({
|
||||
message: err.message,
|
||||
stack: process.env.NODE_ENV === 'production' ? '' : err.stack
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
notFound,
|
||||
errorHandler
|
||||
}
|
Reference in New Issue