How to serve an html page for node.js application using Cloud9 IDE

I have created node.js project using Cloud9 IDE for developing a authentication module which will serve the login page (form.html). I am using mongo db as the database. I have also used the express library for the routing mechanism.

Please find the code details as mentioned below:

app.js:-

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  ,fs = require('fs');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

app.get('/form',function(req,res){
   fs.readFile('.form.html',function(error,content){
       if (error) {
           res.writeHead(500);
           res.end();
       }
       else{
         res.writeHead(200,{'Content-Type':'text/html'});
         res.end(content,'utf-8');
       }
   }); 
});


app.listen(process.env.PORT);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

package.json:-

{
    "name": "authentication"
  , "version": "0.0.1"
  , "private": true
  , "dependencies": {
      "express": "2.5.8"
      , "jade": "0.26.1"
      ,"mongoose":"2.6.5"
  }
}

form.html:-

<form action="/signup" method="post">
<div>
    <label>Username:</label>
    <input type="text" name="username"/><br/>
</div>
<div>
     <label>Password:</label>
    <input type="password" name="password"/><br/>
</div>
<div><input type="submit" value="Sign Up"/></div>
</form>

The above form.html is created within the authentication folder.

For express implementation I have used the following command :

npm install -g @express2.5.8

express authentication

then had updated the package.json file as mentioned above and run the following command:

cd authentication

npm install

node app

After writing all the commands as mentioned above I am not able to see the form.html as the output.

Can anyone help me out to provide some solution in order to serve the form.html page as the output for the application.

Thanks & Regards, Santosh Kumar Patro

Since you are using express.static middleware, just place form.html inside /public directory (or simlink it). Saves a lot of time.

Form will be available at host:port/form.html.

fs.readFile('.form.html' 

needs to be

fs.readFile('form.html' 

remove the leading dot!

what is the value of 'process.env.PORT' which you use in app.listen()? in my example below i used 8080

point your browser to localhost:8080/form