Download presentation
Presentation is loading. Please wait.
Published byMiguel Páez Vázquez Modified over 6 years ago
1
2017, Fall Pusan National University Ki-Joune Li
Node.js 2017, Fall Pusan National University Ki-Joune Li
2
Server Side Programming
Client Side Server Side HTTP request Web Client – Browser (JavaScript) Web Server HTTP response (HTML, CSS, JavaScript, JSON, XML, etc..) ASP.NET, PHP, Node.js, etc..
3
Basic Concepts Node.js Node.js can A Node.js file operates on
uses JavaScript on the server is an open source server framework and free runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) Node.js can generate dynamic page content create, open, read, write, delete, and close files on the server collect form data add, delete, modify data in your databases A Node.js file operates on Certain events (e.g. access a port on the server) Activation on the server before having any effect, Node.js file has an extension “.js”
4
Asynchronous protocol of node.js
Synchronous programming Server Side Client Side Web Server 1. HTTP request (e.g. read a file) Web Client 2. Wait the next request and process it 4. HTTP response 5. Get the results If the server waits only the request from one single client, it is excellent. Otherwise, the waiting is a waste of resources and multi-thread is required (not memory efficient)
5
Asynchronous protocol of node.js
Asynchronous programming Message queue (mailbox) Server Side Client Side Web Server 1. HTTP request (e.g. read a file) Web Client 2. Read next request and process it when ready 3. HTTP response 4. Get the results No reason to wait at the server side, which improves the total utilization of server. Single thread and memory efficient.
6
Node.js installation and running a server
You have to install Node.js® - JavaScript runtime environment Initiate your web server var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('Hello World!'); }).listen(8080) 'Hello World' when there is any access to port 8080
7
Message queue (mailbox)
Example: myfirst.js Message queue (mailbox) myfirst.js Web Browser Web Server 1. HTTP request (simple access) Web Client 2. Read from 8080 port next request and make “hello world!” html message 3. HTTP response 4. Get the html message
8
Node.js Modules Node.js provides a set of built-in modules
Descritpion assert Provides a set of assertion tests buffer To handle binary data child_process To run a child process cluster To split a single Node process into multiple processes crypto To handle OpenSSL cryptographic functions dgram Provides implementation of UDP datagram sockets dns To do DNS lookups and name resolution functions domain Deprecated. To handle unhandled errors events To handle events fs To handle the file system http To make Node.js act as an HTTP server https To make Node.js act as an HTTPS server. net To create servers and clients os Provides information about the operation system path To handle file paths punycode Deprecated. A character encoding scheme querystring To handle URL query strings readline To handle readable streams one line at the time stream To handle streaming data string_decoder To decode buffer objects into strings timers To execute a function after a given number of milliseconds tls To implement TLS and SSL protocols tty Provides classes used by a text terminal url To parse URL strings util To access utility functions v8 vm To compile JavaScript code in a virtual machine zlib To compress or decompress files Node.js provides a set of built-in modules To include a module var http = require('http');
9
HTTP Module Module that allows to transfer data via http as we have already seen var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('Hello World!'); }).listen(8080) 'Hello World' when there is any access to port 8080 Web Browser Web Server 1. HTTP request (simple access) Web Client 2. HTTP response
10
File System Module Module that allows you to work with the file system on your computer. Read files Create files Update files Delete files Rename files var fs = require('fs'); Client Side Server Side HTTP request Web Client – Browser (JavaScript) Web Server File System HTTP response var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { fs.readFile('demofile1.html', function(err, data) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write(data); res.end(); }); }).listen(8080)
11
File System Module Functions fs.appendFile() fs.open() fs.writeFile()
fs.unlink() fs.rename var fs = require('fs'); fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) { if (err) throw err; console.log('Saved!'); });
12
Event Module Module that allows to handle events
To create, fire, or listen on events var events = require('events'); var eventEmitter = new events.EventEmitter(); var events = require('events'); var eventEmitter = new events.EventEmitter(); //Create an event handler: var myEventHandler = function () { console.log('I hear a scream!'); } //Assign the event handler to an event: eventEmitter.on('scream', myEventHandler); //Fire the 'scream' event: eventEmitter.emit('scream');
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.