var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
DateTimeModule.js
exports.myDateTime = function () {
return Date();
};
var http = require('http');
var dt = require('./DateTimeModule');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("The date and time are currently: " + dt.myDateTime());
res.end();
}).listen(8080);
HTMLDemo.html
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('HTMLDemo.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
SplitURL.js
var url = require('url');
var adr = 'http://localhost:8080/default.htm?year=2020&month=october';
var q = url.parse(adr, true);
console.log(q.host); //returns 'localhost:8080'
console.log(q.pathname); //returns '/default.htm'
console.log(q.search); //returns '?year=2020&month=october'
var qdata = q.query; //returns an object: { year: 2020, month: 'october' }
console.log(qdata.month); //returns 'october'
Demo Seasons
var http = require('http');
var url = require('url');
var fs = require('fs');
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
fs.readFile(filename, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
http://localhost:8080/summer.html
<!DOCTYPE html>
<html>
<body>
<h1>Summer</h1>
<p>I love the sun!</p>
</body>
</html>
http://localhost:8080/winter.html
<!DOCTYPE html>
<html>
<body>
<h1>Winter</h1>
<p>I love the snow!</p>
</body>
</html>