Node.
js Setup Document
1. Download .node from [Link].
2. Check in Run the Version
a. npm –v
b. Node-v
Download Visiual Code
Install Extension in visiual code
[Link] Modules Intellisense.
[Link] Extension Pack.
[Link] Code fro [Link]
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: [Link]: 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 [Link] with for us http protocol
{
“[Link]”:true,
“[Link]”:false
}
Press F5 debug
[Link] is an open source server environment.
[Link] allows you to run JavaScript on the server.
What is [Link]?
[Link] is an open source server environment
[Link] is free
[Link] runs on various platforms (Windows, Linux, Unix, Mac OS X,
etc.)
[Link] uses JavaScript on the server
What Can [Link] Do?
[Link] can generate dynamic page content
[Link] can create, open, read, write, delete, and close files on the
server
[Link] can collect form data
[Link] can add, delete, modify data in your database
What is a Module in [Link]?
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');
[Link](function (req, res) {
[Link](200, {'Content-Type': 'text/html'});
[Link]('Hello World!');
}).listen(8080);
[Link] 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-
[Link](function (req, res) {
[Link](200, {'Content-Type': 'text/html'});
[Link]('Hello World!');
}).listen(8080);
3. Add an http header
var http = require('http');
[Link](function (req, res) {
[Link](200, {'Content-Type': 'text/html'});
[Link]('Hello World!');
[Link]();
}).listen(8080);
4. Read the Query String
ar http = require('http');
[Link](function (req, res) {
[Link](200, {'Content-Type': 'text/html'});
[Link]([Link]);
[Link]();
}).listen(8080);
output
url: [Link]
5 Split the Query String
var http = require('http');
var url = require('url');
[Link](function (req, res) {
[Link](200, {'Content-Type': 'text/html'});
var q = [Link]([Link], true).query;
var txt = [Link] + " " + [Link];
[Link](txt);
}).listen(8080);
url : [Link]
ouput: 2017 July
[Link] 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
[Link]
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>
.js
var http = require('http');
var fs = require('fs');
[Link](function (req, res) {
//Open a file on the server and return its content:
[Link]('[Link]', function(err, data) {
[Link](200, {'Content-Type': 'text/html'});
[Link](data);
return [Link]();
});
}).listen(8080);
Output
My Header
My paragraph.
[Link] Files
var fs = require('fs');
[Link]('[Link]', 'Hello content!', function (err) {
if (err) throw err;
[Link]('Saved!');
});
8 .Open a file and save
var fs = require('fs');
//create an empty file named [Link]:
[Link]('[Link]', 'w', function (err, file) {
if (err) throw err;
[Link]('Saved!');
});
Output saved
9. Update Files
var fs = require('fs');
[Link]('[Link]', ' This is my text.', function (err) {
if (err) throw err;
[Link]('Updated!');
});
Output update
10. Delete Files
var fs = require('fs');
[Link]('[Link]', function (err) {
if (err) throw err;
[Link]('File deleted!');
});
Output Deleted
[Link] a file
var fs = require('fs');
//Rename the file "[Link]" into "[Link]":
[Link]('[Link]', '[Link]', function (err) {
if (err) throw err;
[Link]('File Renamed!');
});
Output File Renamed
Install a package
npm install upper-case
C:\Users\My Name\node_modules\upper-case
[Link] Upload
var http = require('http');
var formidable = require('formidable');
var fs = require('fs');
[Link](function (req, res) {
if ([Link] == '/fileupload') {
var form = new [Link]();
[Link](req, function (err, fields, files) {
var oldpath = [Link];
var newpath = Change The Path for store' + [Link];
[Link](oldpath, newpath, function (err) {
if (err) throw err;
[Link]('File uploaded and moved!');
[Link]();
});
});
} else {
[Link](200, {'Content-Type': 'text/html'});
[Link]('<form action="fileupload" method="post"
enctype="multipart/form-data">');
[Link]('<input type="file" name="filetoupload"><br>');
[Link]('<input type="submit">');
[Link]('</form>');
return [Link]();
}
}).listen(8080);