Creating your first Express application
After creating your package.json
file and installing your dependencies, you can now create your first Express application by adding your already familiar server.js
file with the following lines of code:
var express = require('express'); var app = express(); app.use('/', function(req, res) { res.send('Hello World'); }); app.listen(3000); console.log('Server running at http://localhost:3000/'); module.exports = app;
You should already recognize most of the code. The first two lines require the Express module and create a new Express application object. Then, we use the app.use()
method to mount a middleware function with a specific path, and the
app.listen()
method to tell the Express application to listen to the port 3000
. Notice how the module.exports
object is used to return the application object. This will later help us load and test our Express application.
This new code should also be familiar to you...