CS 80: Internet Programming
Instructor: Mark Edmonds
node
at your terminal/command
promptSave the following in
hello_world.js
console.log("Hello, world!");
Launch the program with
node hello_world.js
http_server.js
// basic HTTP server with an export
var http = require("http"); // require the node HTTP module
function onRequest(request, response)
{
console.log("Request received.");
response.writeHead(200,
{
"Content-Type": "text/plain"
}); // set HTTP response header
response.write("Hello World"); // write content into HTTP request
response.end(); // finishes the response
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
var http = require("http");
in
the HTTP server exampleexports
http_server_export.js
// basic HTTP server with an export
var http = require("http"); // require the node HTTP module
function start()
{
function onRequest(request, response)
{
console.log("Request received.");
response.writeHead(200,
{
"Content-Type": "text/plain"
}); // set HTTP response header
response.write("Hello World"); // write content into HTTP request
response.end(); // finishes the response
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
index.js
var server = require("./http_server_export");
server.start();
router.js
// create a router; this could also route to different applications (controllers) based on the file extension, or other information
// here, we just print the request we are routing
function route(pathname)
{
console.log("About to route a request for " + pathname);
}
exports.route = route;
http_server_router.js
var http = require("http"); // require the node HTTP module
var url = require("url");
function start(route)
{
function onRequest(request, response)
{
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(pathname);
response.writeHead(200,
{
"Content-Type": "text/plain"
}); // set HTTP response header
response.write("Request for " + pathname + " received."); // write content into HTTP request
response.end(); // finishes the response
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start
index_router.js
var server = require("./http_server_router");
var router = require("./router");
server.start(router.route);
http_fileserver.js
var http = require('http');
var url = require('url');
var fs = require('fs');
function start(route)
{
function onRequest(request, response)
{
// look for file in current directory
var filename = "." + url.parse(request.url).pathname;
console.log("Request for " + filename + " received.");
fs.readFile(filename, function(err, data)
{
// error getting the file, return a 404
if (err)
{
response.writeHead(404,
{
'Content-Type': 'text/html'
});
return response.end("404 Not Found");
}
// successfully retrieved file
response.writeHead(200,
{
'Content-Type': 'text/html'
});
// write the file's data into the response
response.write(data);
return response.end();
});
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start
index_fileserver.js
var server = require("./http_fileserver");
var router = require("./router");
server.start(router.route);