This package has installed:
- Node.js v8.11.2 to /usr/local/bin/node
- npm v5.6.0 to /usr/local/bin/npm
Make sure that /usr/local/bin is in your $PATH.
mkdir TestProject
cd TestProject
Make this directory a root of your project (this will create a default package.json
file)npm init --yes
Install required npm module and save it as a project dependency (it will appear in package.json
)npm install request --save
Create a test.js
file in project directory with code from package examplevar request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // Print the google web page.
}
});
Your project directory should look like thisTestProject/
- node_modules/
- package.json
- test.js
Now just run node inside your project directorynode test.js
var fs = require('fs');
var rs = fs.createReadStream('./demofile.txt');
rs.on('open', function () {
console.log('The file is open');
});
require()
method. In addition, all event properties and methods are an instance of an
EventEmitter object. To be able to access these properties and methods, create
an EventEmitter object:
var events = require('events');
var eventEmitter = new events.EventEmitter();
emit()
method.
C:\Users\Your Name>npm install upper-case
C:\Users\My Name\node_modules\upper-case
var uc = require('upper-case');
var http = require('http');
var uc = require('upper-case');
http.createServer(function
(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(uc("Hello
World!"));
res.end();
}).listen(8080);
C:\Users\Your Name>node demo_uppercase.js
require()
method:
var url = require('url');
url.parse()
method, and it will return a URL object with each part of the address as
properties:
var url = require('url');
var adr = 'http://localhost:8080/default.htm?year=2017&month=february';
var q = url.parse(adr, true);
console.log(q.host); //returns 'localhost:8080'
console.log(q.pathname); //returns '/default.htm'console.log(q.search); //returns '?year=2017&month=february'
var qdata = q.query; //returns an object: { year: 2017, month: 'february' }
console.log(qdata.month); //returns 'february'
<!DOCTYPE html>
<html>
<body>
<h1>Summer</h1>
<p>I love
the sun!</p>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>Winter</h1>
<p>I love
the snow!</p>
</body>
</html>
var http = require('http');
var url = require('url');
var fs = require('fs');
http.createServer(function
(req, res) {
var q = url.parse(req.url, true);
var
filename = "." + q.pathname;
fs.readFile(filename,
function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
C:\Users\Your Name>node demo_fileserver.js
Summer
I love the sun!
Winter
I love the snow!
require()
method:
var fs = require('fs');
fs.readFile()
method is used to read files on your computer.
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>
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);
C:\Users\Your Name>node demo_readfile.js
require()
method:
var http = require('http');
createServer()
method to create an
HTTP server:
var http = require('http');
//create a server object:http.createServer(function
(req, res) {
res.write('Hello World!'); //write a response to the
client res.end(); //end the response}).listen(8080); //the
server object listens on port 8080
http.createServer()
method, will be executed when someone tries to access the
computer on port 8080.
C:\Users\Your Name>node demo_http.js
require()
function with the name of the module:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
exports.myDateTime = function () {
return Date();
};
exports
keyword to make properties and methods available outside the module file.
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 are currently: " + dt.myDateTime());
res.end();
}).listen(8080);
./
to locate the module, that means that the
module is located in the same folder as the Node.js file.
C:\Users\Your Name>node demo_module.js