Reza,

As the others have said, Express is lightweight, as far as frameworks go. 
If your needs are small, there's no need for a framework, but it will take 
some time to learn the edges and the details of how things work.

You could do it all by hand with the "http" module. Here's the example on 
the nodejs.org home page:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

The problem with this is that it responds to *every* request with "Hello 
World". If you want to serve some static files, say index.html and index.js 
-- to be executed on the client, then you have to do some routing. To do 
the routing manually, without a framework, it would look something like 
this:

var fs = require('fs');
var http = require('http');
http.createServer(function (req, res) {
  if (req.url == '/index.html') {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(fs.readFileSync('index.html'));
  }
  else if (req.url == '/index.js') {
    res.writeHead(200, {'Content-Type': 'application/javascript'});
    res.end(fs.readFileSync('index.js'));
  }
  else if (req.url == '/login.js') {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('To-do: Process the login\n'));
  }
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

This is a less-than-ideal way to serve files because it is synchronous, 
doesn't cache server-side and doesn't send ETag headers so the browser 
can't cache client-side. You could make it async by using "fs.readFile()" 
instead of "fs.readFileSync()" and that would help a little, but it's much 
better to just use a static-file-serving library, like "node-static" or 
"st", because these will do caching, send ETag headers, pipe the file 
contents as they are read from disk, etc, etc.

Here's an example that uses the node-static library to serve everything in 
a "public" subfolder;

var static = require('node-static');
var fileServer = new static.Server('./public', { cache: false });

var http = require('http');
http.createServer(function (req, res) {
  if (req.url == '/login.js') {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('To-do: Process the login\n');
  }
  else {
    fileServer.serve(req, res);
  }
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

If the login credentials are coming in as query-string parameters, you can 
use the built-in "querystring" module -- querystring.parse(req.url) -- but 
it's better to send them via POST parameters, which requires waiting until 
the full request has come in via listening for the "end" event and 
concatenate together each "chunk" of the POST body as it comes in -- 
there's an example of doing this manually and guarding against a rogue 
browser sending an infinitely long request to flood the server's 
memory: http://stackoverflow.com/a/8640308/194758. Or an alternate answer 
that does it with express in just 1 line (with the request body 
automatically parsed): http://stackoverflow.com/a/4296402/194758

There are a lot of things to learn and to think about -- using a framework 
like Express handles these kinds of basic tasks for you so you can get up 
and running quickly, but the downside is that you won't learn some of the 
complexities that are going on under the hood unless you read through the 
Express source and take the time to understand how all the pieces fit 
together. IMO, if your goal is to really learn node at a deep level, it's a 
good exercise to do things manually at first, but if you're just trying to 
get up and running as quickly as possible, you would want to use a 
lightweight framework like Express.

-- peter

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to