As we know, Node.js is just advanced version of server side JavaScript programming , so all file we create in this app will be saved with .js extension, just like any other JavaScript file.
In previous article we have read what is node.js? and why to learn node js development. Now we are going to create Node Js App using Visual Studio Code Editor.
First we create a folder for our app, let's call that "wtr-nodeapp" you can give any name. Remember in node console we can write same dos command for files and folders , Go to the drive you want to create your folder just by typing cd e:
Then create the folder mkdir wtr-nodeapp
$ cd e: $ mkdir wtr-nodeapp
Now follow the following few steps carefully, simple step , but understand how it works with VS Editor
To open a new terminal click on “Terminal” => “New Terminal”.
Before you execute node filename.js
command, make sure you are at the right file location, means the folder path where you have saved the file
As you can see in the above screen, "Hello Avishkar, Welcome to WTR"
is the result of executing the Javascript file.
Now we learn how to use exports in node application.
When we want some function to be accessed outside the module then we need to use exports, suppose in above example we want some functionality to be accessible in different file, then we have to write them with “exports”
keywords, let’s look at the example below
let newpersonname="No-Name"; exports.welcome =function welcomeMessage(newpersonname) { console.log("Hello "+ newpersonname +", Welcome to WTR"); }
Now we call the function written in above file from a different JavaScript file below.
let _sample = require('./sample'); let newName="Gates"; console.log(_sample.welcome(newName));
When we install Node it comes with many Built-in Modules. Here are some of the most commonly used core modules.
You can add any module just by executing npm command like npm install package_name
.
for example, execute following command to install express module.
npm install express
Create Dynamic Content from JavaScript Server Side
We need to include some module for this task
loader.js
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World! this is my new page cntent'); }).listen(8080);
Fs module is used when we want to work with file system, like creating, reading, editing, deleting file from server, here i have given a small example of reading file in node application.
In my application I have a file called “content.txt” , in application we will be reading the content using fs module
I have created a small JavaScript file called fso.js (you can give any name) with following code, then calling the file in node terminal to see the result.
const fs = require('fs'); fs.readFile('content.txt', 'utf-8', (err, data) => { if(err) { throw err; } console.log('data: ', data); });
To call any JavaScript file, go to file location and execute the node command like example below.
node fso.js
Learn more about each module in following links: