Skip to content

Commit 32e80ba

Browse files
committed
1 parent b89ee86 commit 32e80ba

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

nodejs/first/myfirst.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
You can run this as a server application with node, thus:
3+
4+
node myfirst.js
5+
6+
Then you can access it over http by opening the url http://localhost:8080 in your browser
7+
8+
This gets a page with:
9+
10+
Hello Javascript Server world!
11+
12+
Note: if you try using https you may see this:
13+
Error code: SSL_ERROR_RX_RECORD_TOO_LONG
14+
*/
15+
16+
var http = require('http');
17+
18+
var msg = 'Hello Javascript Server world!';
19+
20+
/*
21+
var handler = function(req, res){
22+
console.log('req.url=' + req.url);
23+
if(req.url === '/favicon.ico') {
24+
console.log('Favicon was requested');
25+
}
26+
console.log('Sending response: '+msg);
27+
res.writeHead(200, {'Content-Type' : 'text/html'});
28+
res.end(msg);
29+
}
30+
*/
31+
32+
http.createServer(function(req, res){
33+
console.log('req.url=' + req.url);
34+
if(req.url === '/favicon.ico') {
35+
// chrome has a bug, calls url again for favicon..
36+
console.log('Favicon was requested');
37+
}
38+
console.log('Sending response: '+msg);
39+
res.writeHead(200, {'Content-Type' : 'text/html'});
40+
res.end(msg);
41+
}).listen(8080);
42+
43+

nodejs/first/myfirstmodule.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
exports.myDateTime = function(){
3+
4+
var d = Date();
5+
console.log('myDateTime : ' + d);
6+
return d;
7+
8+
}

nodejs/first/myhttps.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var https = require('https');
2+
3+
// This does NOT work just out of the box..
4+
// Docs at https://www.w3schools.com/nodejs/ref_https.asp MISLEADING
5+
//
6+
https.createServer(function (req, res) {
7+
res.writeHead(200, {'Content-Type': 'text/plain'});
8+
res.write('Hello World!');
9+
res.end();
10+
}).listen(8080);

nodejs/first/mymodular.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// See myfirst.js for intro
2+
3+
var http = require('http');
4+
var url = require('url');
5+
var mymodule = require('./myfirstmodule');
6+
7+
/* This error appears when there is no matching module file
8+
Note the actual line 4 is mentioned TWELVE LINES into the output!
9+
10+
C:\dev\nodejs\first>node mymodular.js
11+
internal/modules/cjs/loader.js:985
12+
throw err;
13+
^
14+
15+
Error: Cannot find module './myfirstmodule'
16+
Require stack:
17+
- C:\dev\nodejs\first\mymodular.js
18+
 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
19+
 at Function.Module._load (internal/modules/cjs/loader.js:864:27)
20+
 at Module.require (internal/modules/cjs/loader.js:1044:19)
21+
 at require (internal/modules/cjs/helpers.js:77:18)
22+
at Object.<anonymous> (C:\dev\nodejs\first\mymodular.js:4:16)*/
23+
24+
var hello = 'Modular Javascript Server world! ';
25+
26+
http.createServer(function(req, res){
27+
console.log('req.url=' + req.url);
28+
if(req.url === '/favicon.ico') {
29+
// chrome has a bug, calls url again for favicon..
30+
console.log('Favicon was requested');
31+
}
32+
var msg = req.url + ' requested at: <br>' + mymodule.myDateTime();
33+
console.log('Sending response: '+msg);
34+
res.writeHead(200, {'Content-Type' : 'text/html', 'connection':'close'});
35+
res.end(msg);
36+
}).listen(8080);
37+
38+

0 commit comments

Comments
 (0)