Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

host multiple pages on nodejs

Tags:

node.js

So I have my app at http://localhost:8080/

How can I have http://localhost:8080/subpage ? Because it seems like any page that hits :8080 pulls the server.js

thanks!

** edit - here's what worked for me (thanks to stewe's answer) **

var app = require('http').createServer(createServer);
var fs = require('fs'); 
var url = require('url');

function createServer(req, res) {
    var path = url.parse(req.url).pathname;
    var fsCallback = function(error, data) {
        if(error) throw error;

        res.writeHead(200);
        res.write(data);
        res.end();
    }

    switch(path) {
        case '/subpage':
            doc = fs.readFile(__dirname + '/subpage.html', fsCallback);
        break;
        default:
            doc = fs.readFile(__dirname + '/index.html', fsCallback);
        break;
    }
}

app.listen(8080);
like image 589
Jacksonkr Avatar asked Sep 03 '11 02:09

Jacksonkr


People also ask

Can NodeJS handle multiple requests?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.

How many TCP connections can NodeJS handle?

In any case I don't think that is the issue, becasue when I run the same nodejs server process (on a different port), the two processes are able to handle 32K concurrent connections each (64K total). Thanks for your help. Groups "nodejs" group.

Is Node js good for scaling?

Scaling your Node. js application horizontally across multiple machines is similar to scaling across multiple cores on a single machine. As long as your application can run as an independent process, it can be distributed to run across several machines.

Can you use node js to host a website?

If you're running a very small website, it may be worth it to save some money with a shared hosting plan. That being said, Node. js is much more compatible with the private server environment of a VPS or a dedicated server, and you'll get a lot more out of the experience.


2 Answers

Here is a start:

var http=require('http');
var url=require('url');

var server=http.createServer(function(req,res){
    var pathname=url.parse(req.url).pathname;
    switch(pathname){
        case '/subpage':
            res.end('subpage');
        break;
        default:
            res.end('default');
        break;
    }

}).listen(8080);
like image 168
stewe Avatar answered Sep 27 '22 22:09

stewe


I hit the same problem as you did, and I think what we were both looking for is basically a routing engine for node.js. Basically, okay fine I get the hello-world example for nodejs, but how do I build something that responds to different requests?

For future users who land on this page via google, you must look at Express.js and this excellent guide and intro into express, Understanding Express.js. These two will solve the problem

like image 29
Sudipta Chatterjee Avatar answered Sep 27 '22 22:09

Sudipta Chatterjee