-
Notifications
You must be signed in to change notification settings - Fork 0
02 hello express
The package.json file specifies that our app requires the express() module.
{
"dependencies": {
"express": "^4.13.3"
}
}
The npm install command reads package.json and sees that we require express. Then it installs express and all of its dependencies. The dependencies (source code files) are placed in the node_modules directory.
Install dependencies:
npm install
Run the app:
nodejs hello-express.js
Run the app under the debugger:
nodejs debug hello-express.js
In the browser:
http://localhost:8080
Stop the app:
CTRL-C
The call to require('express') returns the value of the module.exports object in the express.js module. We assign the exports to the variable e. Then we use e like a constructor to get an application object, which we assign to the variable a. TODO: Investigate this constructor-like thing.
var e = require('express');
var a = e();
We call a.get to associate an anonymous handler with HTTP GET '/'. Our handler sends a response of Hello Steve.
a.get('/', function(req, res) {
res.send('Hello, Steve!');
});
We call a.listen to start a local web server that listens on port 8080.
a.listen(8080);