Node JS Resorce File
Node JS Resorce File
LAURENCE SVEKIS
- Over 300 courses in technology and
web applications.
- 20 years of web programming
experience
- 600,000+ students across multiple
platforms
- Digital instructor since 2002
node test.js
Open your editor and create a js file that
npm install npm@latest -g
contains console.log('Hello World'); save
it as test.js
In the terminal type node test.js and
watch for a result. What gets returned?
NPM - check if its installed npm -v latest
version install npm install npm@latest -g
global.console.log('hello');
https://nodejs.org/api/globals.html
console.log(global);
require(‘path’); console.log(process.pid);
console.log(__dirname);
Process objects - version , pid , env console.log(__filename);
console.log(process.argv);
first.js
randomNum.js
//console.log(module);
const fs = require('fs');
https://nodejs.org/api/fs.html
const myFiles = fs.readdirSync('./mods');
console.log(myFiles);
fs.readdir('./mods',(error,files)=>{
if(error){throw error;}
console.log('readdir');
console.log(files);
})
fs.readFile('./mods/test.txt','UTF-8',(error,output)=>{
if(error){throw error;}
console.log(output);
});
*/
fs.readFile('./mods/index.html', (error, html) => {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.write(html);
response.end();
})
});
server.listen(port, host, () => {
console.log('server running');
})
const http = require('http');
Reading files and folders const fs = require('fs');
const url = require('url');
const port = 8080;
const host = '127.0.0.1';
const server = http.createServer((request, response) => {
const base = url.parse(request.url);
//console.log(base);
const fileName = './mods' + base.pathname;
fs.readFile(fileName, (error, html) => {
if (error) {
response.writeHead(404, {
'Content-Type': 'text/html'
});
response.end(`<h1>Not Found</h1>`);
}
else {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.write(html);
response.end();
}
})
})
server.listen(port, host, () => {
console.log('ready');
})
https://lodash.com/
$ sudo npm i -g npm
https://www.npmjs.com/package/lodash $ sudo npm i --save lodash