First i am create
Create package.json file with following code
var express = require('express') , app = express.createServer();
app.get('/', function(req, res) { res.send('hello world'); });
app.listen(3000)
Then install npm using npm install in code folder.
Here is a "Hello World" example, extracted from the Express guide, suggested above:
var express = require('express');
var app = express(); // here I use the express() method, instead of the createServer()
app.get('/', function(req, res){
res.send('Hello World');
});
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
You will want to put the code inside a file called app.js
instead of package.json
. Then you should be able to just run the app using node app.js
. The package.json
can be created with npm init
and npm --save express
.
Or you can use the express-generator
tool as described in the Express guide.
var express = require('express');
var app = express();
var http = require('http');
app.set('port', process.env.PORT || 3000);
app.get('/', function(req, res){
res.send('Hello World');
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});