express-backend-server
v0.0.11
Published
Express backend server.
Downloads
10
Maintainers
Readme
Express Backend Server
Built-in Modules
Built-in Utility Modules
Getting Started
1. Install
With npm do:
npm install --save express-backend-server
2. Example Usage
Includes
CommonJS:
const { EBServer } = require('express-backend-server');
ES Modules:
import { EBServer } from 'express-backend-server';
Simple Server Configuration And Startup
const backendServer = new EBServer({
clients: { // Optional.
sentry: { // Optional.
// Set Sentry module options.
dsn: 'set_sentry_dsn_url' // Your Sentry dsn url.
}
},
httpServer: { // Required.
port: 9000, // Required.
host: 'localhost', // Optional.
viewEngine: 'pug', // Optional. if you use, do not forget to install your view engine module.
viewsFolderPath: 'view', // Optional.
publicFolderPath: 'public', // Optional.
bodyParser: { // Optional.
// Set Body Parser module options.
json: { // Optional.
use: true
// Set Body Parser Json options.
},
urlencoded: { // Optional.
use: true,
options: { // Optional.
// Set Body Parser Url Encoded options.
extended: false
}
}
},
cors: { }, // Optional. Set Cors module options or use cors: true for default options.
helmet: { }, // Optional. Set Helmet module options or use helmet: true for default options.
compression: { }, // Optional. Set Compression module options or use compression: true for default options.
cookieParser: { // Optional.
use: true
// Set Cookie Parser module options.
},
morgan: { // Optional
// Set Morgan module options.
format: 'dev'
},
importRoutesFunction: function(server) {
const mainRouter = server.middlewares.router.createRouter();
const wwwRouter = server.middlewares.router.createRouter();
const apiRouter = server.middlewares.router.createRouter();
wwwRouter
.get('/', function(req, res) {
res.send('Hello from WWW router. Path /')
})
.get('/test', function(req, res) {
res.send('WWW route test result. Path /test')
});
apiRouter
.get('/', function(req, res) {
res.json({ message: 'Hello from API router.', path: 'api/'})
})
.get('/test', function(req, res) {
res.json({ message: 'API route test result.', path: 'api/test'})
});
mainRouter
.use('/', wwwRouter)
.use('/api', apiRouter);
return mainRouter
}
}
});
backendServer.on('httpServerListening', () => {
console.log(`\nHTPP Server is ready at http://localhost:9000`);
})
.on('httpServerClose', () => {
console.log(`\nHTPP Server closed.`);
})
.on('httpServerError', (err, eventId) => {
console.log(`\nHTPP Server Error (${eventId}) : ${err}`);
})
.start(() => {
console.log(`\nHTTP Server is runing.`)
});
Navigate and see results:
http://localhost:9000/
- Result >>> Hello from WWW router. Path /
http://localhost:9000/test
- Result >>> WWW route test result. Path /test
http://localhost:9000/api
- Result >>> {"message":"Hello from API router.","path":"/api"}
http://localhost:9000/api/test
- Result >>> { "message": "API route test result.", path: "/api/test"}
Advanced Server Config And Startup
In preparation...