Getting Started with Node.JS on Alibaba Cloud

Prominent Node.JS Features

Node Is Asynchronous and Event-Driven

Node Is Very Fast

Node Is Scalable

No Buffering

Installing Node.JS

Read-Eval-Print Loop

How to Launch Node.js Scripts

$ node -e "console.log(new Date());"

Process Information in Node.js

ps aux | grep 'node'
node -e "console.log(process.pid)"

How to Access Global Scope in Node.js

How to Export and Import Modules

exports.name = object;, e.g., 
var messages = {
find: function(req, res, next) {
...
},
add: function(req, res, next) {
...
},
format: 'title | date | author'
}
exports.messages = messages;
var messages = require('./routes/messages.js');
module.exports = function(app) {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
return app;
}
...
var app = express();
var config = require('./config/index.js');
app = config(app);
...
var = express(); require('./config/index.js')(app);.
var keys = require('./keys.js'),
messages = require('./routes/messages.js');

Buffer in Node.js

__dirname versus process.cwd

Other Useful Utilities

  1. URL
  2. Crypto
  3. Path
  4. String Decoder

Handling File Systems in Node.js

var fs = require('fs');
var path = require('path');
fs.readFile(path.join(__dirname, '/data/customers.csv'), {encoding: 'utf-8'}, function (err, data) {
if (err) throw err;
console.log(data);
});
var fs = require('fs');
fs.writeFile('message.txt', 'Hello World!', function (err) {
if (err) throw err;
console.log('Writing is done.');
});

How to Stream Data in Node.js

var fs = require('fs');
fs.createReadStream('./data/clients.csv').pipe(process.stdout);

Using NPN to Install Node.js modules

  1. The package.json file
  2. The node_modules folder

Hello World

var HTTP = require('HTTP');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1332, '127.100.10.1');
console.log('Server running at http://127.100.10.1:1332/');]

Catching Bugs in Node.js

Dealing with Callbacks in Node.js

fs.readdir(source, function(err, files) {
if (err) {
console.log('Error finding files: ' + err)
} else {
files.forEach(function(filename, fileIndex) {
console.log(filename)
gm(source + filename).size(function(err, values) {
if (err) {
console.log('Error identifying file size: ' + err)
} else {
console.log(filename + ' : ' + values)
aspect = (values.width / values.height)
widths.forEach(function(width, widthIndex) {
height = Math.round(width / aspect)
console.log('resizing ' + filename + 'to ' + height + 'x' + height)
this.resize(width, height).write(destination + 'w' + width + '_' + filename, function(err) {
if (err) console.log('Error writing file: ' + err)
})
}.bind(this))
}
})
})
}
})

Summary

  1. I/O bound apps
  2. Apps for data streaming
  3. Data Intensive Real-time Applications (DIRT)
  4. JSON APIs based apps
  5. Single page apps

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Inside the V8 Engine

The Sessions Curiosity Rails React App

Configuring Bind9 for Name Server records to make your domain reachable from the INTERNET.

Drupal and Vue.js: Playing Together to Craft Rich Web Experiences

Fresh Visual Studio Code, well working Bootstrap and more — Frontend News #16

Dingulo: Keep track of your days and have a better insight about your life

Dependency Injection in Node.js — Higher Order Routing With Express

OpenReplay

Getting Started with Node.js

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Overview of ORM Database Tools

The PERN stack with Docker for beginners

WAI261 Tutorial 0 — Setting up VSCode, Git and NodeJS

Setting up Nestjs with PostgreSQL