An Advanced Intro to NodeJS (with Express) 11-24-2018, 10:11 PM
#1
Introduction
Like my other tutorials, this will be done with Ubuntu through the terminal. Nodejs is great for building scalable web applications because it doesn't make I/O calls, preventing thread blocking in the underlying OS. NodeJS makes heavy use of callbacks. In software programming, callbacks are basically function that are given as arguments to other functions and called when an event happens.
Installation
Install nodejs and npm, at the time of writing this I believe node 10 is the current dev release and node 8 is the stable release. I chose to use the stable node 8 release (https://deb.nodesource.com/setup_8.x).
Your First APP
You can now create a folder and name it whatever you want, I will name my folder nodefirst. My folder is also located in my web root folder, but you can place it anywhere you would like on the OS. After you create the folder, type npm init to initialize the directory as a new node project. There will be several prompts, you can just hit enter until it's done. This will generate a package.json file in the directory.
Server.js
Create a file named server.js and enter the following code into it:
Now, you can run node with the server.js file.
You can now visit http://localhost:1234/ to see your Hello World. But that's not very exciting is it? It never is. The code above doesn't sit very well with me since it's not a proper callback. We can rewrite this code above and make it a proper callback and alleviate my programming OCD.
So now, in the code above, the callback function is newServer. It is important that you understand how to write and understand what callback functions are. Callbacks are very important when building scalable applications in NodeJS.
Routing and Express
When it comes to routing, you can use the url library, but I prefer express. Since we are using Express we don't need some of the earlier code we had. In fact, we won't need any of it after installing Express.
To use the express lib you will need to install it using an npm command. npm install express --save
After it's installed we can start using it. Below is a super basic example of routing with Express. You will notice that when we included the Express library we didn't need any of the previous code. It basically allowed us to write less code and get our application to a working example much faster.
So, if we wanted to route another page, like and about page, we could simply add the following code.
Serving HTML Files
If we continued on with how we were doing it above, it would get quite confusing and the file would be super huge. Instead, we can make an HTML file and serve it to our user when they make the request to the server. So now, create a new directory named html and create a file in that directory named about.html. In this about HTML file, I only have one line, it says this is the about html file. To serve this file when a user requests it we will use the sendFile method:
Like my other tutorials, this will be done with Ubuntu through the terminal. Nodejs is great for building scalable web applications because it doesn't make I/O calls, preventing thread blocking in the underlying OS. NodeJS makes heavy use of callbacks. In software programming, callbacks are basically function that are given as arguments to other functions and called when an event happens.
Installation
Install nodejs and npm, at the time of writing this I believe node 10 is the current dev release and node 8 is the stable release. I chose to use the stable node 8 release (https://deb.nodesource.com/setup_8.x).
Code:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get -y install npm nodejsYour First APP
You can now create a folder and name it whatever you want, I will name my folder nodefirst. My folder is also located in my web root folder, but you can place it anywhere you would like on the OS. After you create the folder, type npm init to initialize the directory as a new node project. There will be several prompts, you can just hit enter until it's done. This will generate a package.json file in the directory.
Server.js
Create a file named server.js and enter the following code into it:
Code:
const http = require('http'), server = http.createServer();
server.on('request',(request,response)=>
{
response.writeHead(200,{'Content-Type':'text/plain'});
response.write('Hello world');
response.end();
});
server.listen(1234,()=>
{
console.log('Node server created at port 1234');
});Now, you can run node with the server.js file.
Code:
(xenial)sunjester@localhost:/var/www/html/nodefirst$ nodejs server.js
Node server created at port 1234You can now visit http://localhost:1234/ to see your Hello World. But that's not very exciting is it? It never is. The code above doesn't sit very well with me since it's not a proper callback. We can rewrite this code above and make it a proper callback and alleviate my programming OCD.
Code:
const http = require('http');
newServer = function(request, response)
{
response.writeHead(200,{'Content-Type':'text/plain'});
response.write('Hello world');
response.end();
}, server = http.createServer(newServer);
server.listen(1234, ()=>
{
console.log('server started on port 1234');
});So now, in the code above, the callback function is newServer. It is important that you understand how to write and understand what callback functions are. Callbacks are very important when building scalable applications in NodeJS.
Routing and Express
When it comes to routing, you can use the url library, but I prefer express. Since we are using Express we don't need some of the earlier code we had. In fact, we won't need any of it after installing Express.
To use the express lib you will need to install it using an npm command. npm install express --save
Code:
(xenial)sunjester@localhost:/var/www/html/nodefirst$ npm install express --save
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN nodefirst@1.0.0 No description
npm WARN nodefirst@1.0.0 No repository field.
+ express@4.16.4
added 48 packages from 36 contributors and audited 121 packages in 17.201s
found 0 vulnerabilitiesAfter it's installed we can start using it. Below is a super basic example of routing with Express. You will notice that when we included the Express library we didn't need any of the previous code. It basically allowed us to write less code and get our application to a working example much faster.
Code:
const express = require('express');
var app = express();
app.listen(1234);
app.get('/', function(request, response)
{
response.send('hello world');
});So, if we wanted to route another page, like and about page, we could simply add the following code.
Code:
app.get('/about', function(request, response)
{
response.send('the about page');
});Serving HTML Files
If we continued on with how we were doing it above, it would get quite confusing and the file would be super huge. Instead, we can make an HTML file and serve it to our user when they make the request to the server. So now, create a new directory named html and create a file in that directory named about.html. In this about HTML file, I only have one line, it says this is the about html file. To serve this file when a user requests it we will use the sendFile method:
Code:
app.get('/about', function(request, response)
{
response.sendFile(__dirname + "/html/about.html");
});
(This post was last modified: 11-24-2018, 10:12 PM by sunjester.)




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)


