node-express-mongodb-jwt-rest-api-skeleton
Version:
Node.js express.js MongoDB JWT REST API - This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API)
43 lines (36 loc) • 992 B
JavaScript
const express = require('express')
const router = express.Router()
const fs = require('fs')
const routesPath = `${__dirname}/`
const { removeExtensionFromFile } = require('../middleware/utils')
/*
* Load routes statically and/or dynamically
*/
// Load Auth route
router.use('/', require('./auth'))
// Loop routes path and loads every file as a route except this file and Auth route
fs.readdirSync(routesPath).filter((file) => {
// Take filename and remove last part (extension)
const routeFile = removeExtensionFromFile(file)
// Prevents loading of this file and auth file
return routeFile !== 'index' && routeFile !== 'auth' && file !== '.DS_Store'
? router.use(`/${routeFile}`, require(`./${routeFile}`))
: ''
})
/*
* Setup routes for index
*/
router.get('/', (req, res) => {
res.render('index')
})
/*
* Handle 404 error
*/
router.use('*', (req, res) => {
res.status(404).json({
errors: {
msg: 'URL_NOT_FOUND'
}
})
})
module.exports = router