Please enable JavaScript.
Coggle requires JavaScript to display documents.
W11 - JS Frameworks [55/67] (Node.js (Simple Web Server (var http =…
W11 - JS Frameworks [55/67]
Node.js
Advantages
asynchronous
which means it
can do more than one thing
at a time
same programming language
on client and server side.
Easy to move functionality
Many third party modules
Simple Web Server
var http = require("http")
var myServer = http.createServer( function(request, response){
. reponse.writeHead(200, {'Content-Type':'text/html'});
. response.write('Hello World');
. response.end();
})).listen(8080);
request with field that contains URL of request
request.url
URL, Query String And Path
Reading Query String
add the following LOC
var url = require('url');
var tmp = request.url;
var urlObj = url.parse(tmp, true);
var query = urlObj.query;
//Output query key-value pairs
response.write("Queries:<br>");
.
response.write(key + ": " + query(key));
for (var key in query){
}
response.end( )
Uses
can use
information contained in URL
to deliver
different pages
to user
Pages can be loaded
from file system or dynamically
using database
Can use
URL information
to deliver
web service in JSON format
Frameworks for Node.js
Express
Installing Express
create folder
mkdir myapp
Navigate to folder
cd myapp
Initialise app:
npm init
Install Express
npm install express --save
Serving Static Files
var express = require('express')
var app = express()
app.use(express.static('public');
app.listen(8080);
Express Web Service
[42/43]