100% found this document useful (1 vote)
119 views

Node

This document provides an overview of full stack architecture using Node.js, Express.js, and Angular. It discusses setting up a Node.js and Express backend to handle routing and requests. It also covers creating an Angular frontend and implementing client-server communication between the frontend and backend using HTTP requests. Examples are given for setting up a basic web server and making requests to retrieve and display web pages using Node.js modules like http, fs, and url.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
119 views

Node

This document provides an overview of full stack architecture using Node.js, Express.js, and Angular. It discusses setting up a Node.js and Express backend to handle routing and requests. It also covers creating an Angular frontend and implementing client-server communication between the frontend and backend using HTTP requests. Examples are given for setting up a basic web server and making requests to retrieve and display web pages using Node.js modules like http, fs, and url.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Full stack Architecture

Node.js and express.js


Web Application
• npm install –g @angular/cli
• npm install -g [email protected]
• npm install -g typescript
• mkdir Angular2-express
• cd Angular2-express
• Create package.json for dependencies) and
app.js (for bootstrapping) for our node.js
app
Client-Server Communication
Client.js
var net = require('net');
var client = net.connect({port: 8080}, function() {
console.log('connected to server!');
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('disconnected from server');
});
Client-Server Communication
Server,js
var net = require('net');
var server = net.createServer(function(connection) {
console.log('client connected');
connection.on('end', function() {
console.log('client disconnected');
});
connection.write('Hello World!\r\n');
connection.pipe(connection);
});
server.listen(8080, function() {
console.log('server is listening');
});
Creating a Web module
var http = require('http');
var fs = require('fs');
var url = require('url');
// Create a server
http.createServer( function (request, response) {
// Parse the request containing file name
var pathname = url.parse(request.url).pathname;
// Print the name of the file for which request is made.
console.log("Request for " + pathname + " received.");
// Read the requested file content from file system
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {'Content-Type': 'text/html'});
}else{
//Page found
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/html'});
// Write the content of the file to response body
response.write(data.toString());
}
// Send the response body
response.end();
});
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
Index.html
<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>
Creating a Web client using Node
var http = require('http');
// Options to be used by request
var options = {
host: 'localhost',
port: '8081',
path: '/index.htm‘ };
// Callback function is used to deal with response
var callback = function(response){
// Continuously update stream with data
var body = '';
response.on('data', function(data) {
body += data; });
response.on('end', function() {
// Data received completely.
console.log(body);
}); }
// Make a request to the server
var req = http.request(options, callback);
req.end();
index.html
<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>
Express.js

You might also like