0% found this document useful (0 votes)
20 views4 pages

Nodejs Prac

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Nodejs Prac

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

https://nodejs.

org

var http = require('http');

http.createServer(function (req, res) {


res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);

http://localhost:8080/

The date and time is currently: Mon Dec 02 2024 12:51:29 GMT+0530 (India
Standard Time)

"myfirstmodule.js"
exports.myDateTime = function () {
return Date();
};

var http = require('http');


var dt = require('./myfirstmodule');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("The date and time is currently: " + dt.myDateTime());
res.end();
}).listen(8080);
Node.js as a File Server

the File System module, use the require() method:

var fs = require('fs');

Common use for the File System module:

 Read files
 Create files
 Update files
 Delete files
 Rename files

Example:Read Files

The fs.readFile() method is used to read files on your computer.

demofile1.html
<html><body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body></html>

"demo_readfile.js"

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);
return res.end();
});
}).listen(8080);

Node.js NPM
NPM to download the package you want.
C:\Users\Your Name>npm install upper-case

Now you have downloaded and installed your first package!

NPM creates a folder named "node_modules", where the package will be


placed. All packages you install in the future will be placed in this folder.

My project now has a folder structure like this:

C:\Users\My Name\node_modules\upper-case

Using a Package

var uc = require('upper-case');

Create a Node.js file that will convert the output "Hello World!" into upper-case
letters:

var http = require('http');


var uc = require('upper-case');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
/*Use our upper-case module to upper case a string:*/
res.write(uc.upperCase("Hello World!"));
res.end();
}).listen(8080);

STEP1 ON cmd
C:\Users\Your Name>node demo_uppercase.js

STEP2:ON BROWSER.
http://localhost:8080
OUTPUT:
HELLO WORLD!

You might also like