Cannot run index.js in nodejs with express

First i am create

  1. Code folder in c: drive
  2. Then navigate Code in cmd
  3. 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)

  4. Then install npm using npm install in code folder.

  5. Then code have new folder node_modules and package.json file. Inside node_modules folder express folder is available inside 2 folder is available lib and node_modules and many more files is there. Now i am confused to run my first project.

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'));
});