Bootstrap an express application

Things we perform in a express application’s entry point file:

  1. Import dependencies.
  2. Configurations. Such as the template engine.
  3. Define Middlewares.
  4. Define Routes.
  5. Connection to DB.
  6. Start the app.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Dependencies.
var expres = require('express');
var app = express();
// Configurations.
var port = process.env.PORT || 7654;
app.set('port', port);
app.set('view', __dirname + '/views');
// Middlewares.
app.use(express.favicon());
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session());
// Routes.
app.get('*', function (req, res) {
res.end('Hello World!');
});
// DB Connection.
// Start.
app.listen(port, function () {
console.log('Service is running on: ', port);
});
0%