Skip to content

Latest commit

 

History

History
47 lines (29 loc) · 1.36 KB

separateexpress.md

File metadata and controls

47 lines (29 loc) · 1.36 KB

Separate Express 'app' and 'server'



One Paragraph Explainer

The latest Express generator comes with a great practice that is worth to keep - the API declaration is separated from the network related configuration (port, protocol, etc). This allows testing the API in-process, without performing network calls, with all the benefits that it brings to the table: fast testing execution and getting coverage metrics of the code. It also allows deploying the same API under flexible and different network conditions. Bonus: better separation of concerns and cleaner code



Code example: API declaration, should reside in app.js

var app = express();
app.use(bodyParser.json());
app.use("/api/events", events.API);
app.use("/api/forms", forms);



Code example: Server network declaration, should reside in /bin/www

var app = require('../app');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

Example: test your API in-process using supertest (popular testing package)

alt text