Node.
js Setup Document
1. Download .node from Node.org.
2. Check in Run the Version
a. npm –v
b. Node-v
Download Visiual Code
Install Extension in visiual code
1.Node.js Modules Intellisense.
2.Node.js Extension Pack.
3.Vs Code fro Node.js
After create the program
Its for create for new json file in new project every time
Command in terminal
npm init
After Command initialize
Package Name: Press Enter
Version 1.0: Press Enter
Describtion: Press Enter
Entry point: Index.js: Press Enter
Git: Press Enter
Keyword: Press Enter
Author: any name: Press Enter
ISC: Press Enter
Run the Command through node and filename with .js
Create folder .vscode and file setting.json with for us http protocol
{
“javascript.validate.enable”:true,
“javascript.suggestions.enabled”:false
}
Press F5 debug
Node.js is an open source server environment.
Node.js allows you to run JavaScript on the server.
What is Node.js?
Node.js is an open source server environment
Node.js is free
Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X,
etc.)
Node.js uses JavaScript on the server
What Can Node.js Do?
Node.js can generate dynamic page content
Node.js can create, open, read, write, delete, and close files on the
server
Node.js can collect form data
Node.js can add, delete, modify data in your database
What is a Module in Node.js?
Consider modules to be the same as JavaScript libraries.
A set of functions you want to include in your application.
Program
1. Hello World
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
2.Include of module
To include a module, use the require() function with the name of the module
Ex:- var http = require('http');
Now your application has access to the HTTP module, and is able to create a
server:
Ex-
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
3. Add an http header
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello World!');
res.end();
}).listen(8080);
4. Read the Query String
ar http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(req.url);
res.end();
}).listen(8080);
output
url: http://localhost:8080/summer
5 Split the Query String
var http = require('http');
var url = require('url');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
var q = url.parse(req.url, true).query;
var txt = q.year + " " + q.month;
res.end(txt);
}).listen(8080);
url : http://localhost:8080/?year=2017&month=July
ouput: 2017 July
6.Node.js File System Module
var fs = require('fs');
Common use for the File System module:
Read files
Create files
Update files
Delete files
Rename files
Read File
demofile1.html
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>
.js
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
//Open a file on the server and return its content:
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
Output
My Header
My paragraph.
7.Create Files
var fs = require('fs');
fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
8 .Open a file and save
var fs = require('fs');
//create an empty file named mynewfile2.txt:
fs.open('mynewfile2.txt', 'w', function (err, file) {
if (err) throw err;
console.log('Saved!');
});
Output saved
9. Update Files
var fs = require('fs');
fs.appendFile('mynewfile1.txt', ' This is my text.', function (err) {
if (err) throw err;
console.log('Updated!');
});
Output update
10. Delete Files
var fs = require('fs');
fs.unlink('mynewfile2.txt', function (err) {
if (err) throw err;
console.log('File deleted!');
});
Output Deleted
11.Rename a file
var fs = require('fs');
//Rename the file "mynewfile1.txt" into "myrenamedfile.txt":
fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {
if (err) throw err;
console.log('File Renamed!');
});
Output File Renamed
Install a package
npm install upper-case
C:\Users\My Name\node_modules\upper-case
12.File Upload
var http = require('http');
var formidable = require('formidable');
var fs = require('fs');
http.createServer(function (req, res) {
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var oldpath = files.filetoupload.path;
var newpath = Change The Path for store' + files.filetoupload.name;
fs.rename(oldpath, newpath, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
});
});
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="fileupload" method="post"
enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
}).listen(8080);