Created by
npm install npm@latest -g
$ node --version
# node -v
# you should see the installed node version
Reference: Visualization of the flow of work in Node.js
const fs = require('fs');
let fileName = './someData.txt';
function blockingRead(fileName) {
const data = fs.readFileSync(fileName, 'utf8');
// blocks here until file is read
console.log(`Sync data read:`);
console.log(data);
}
function nonBlocking(fileName) {
fs.readFile(fileName,'utf8', (err, data) => {
if (err) {
throw err;
}else{
console.log(`Async data read:`);
console.log(data);
}
});
// the callback will be executed when data are readed.
}
console.log(`~~~ 1 ~~~ `);
blockingRead(fileName);
console.log(`~~~ 2 ~~~ `);
console.log(`~~~ 3 ~~~ `);
nonBlocking(fileName);
console.log(`~~~ 4 ~~~ `);
const http = require('http');
const hostname = '127.0.0.1';
const port = process.env.PORT || 8080;
const server = http.createServer((req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('Hello from My Server!\n')
});
server.listen(port, hostname, () => {
console.log(`Nodejs server is running at http://${hostname}:${port}/`)
});
# you may need sudo if you've installed node globally
npm install npm@latest -g
#initialize a npm project (-y == defaults for package.json)
npm init -y
package.json
enables you to share your project with other developers without sharing all the node packages (the dependencies).npm install
on the command line.npm install
script takes all the dependencies listed in the package.json file and installs them in the node_modules folder
# Install package locally
npm install <package>
# Install package globally
npm install -g <package>
These slides are based on
customised version of
framework