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);
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.
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.
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.
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.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With