Create first Node App using VS Code Editor

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

Create a simple NodeJs Application

Now follow the following few steps carefully, simple step , but understand how it works with VS Editor

  • 1. Click on explorer, then create a file called app.js (you can give any name)
  • 2. Write simple javascript in the file.
  • 3. Save the file ( File=> Save), Don't forget
  • 4. Now call the file from console just by typing node app.js

create node js app

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.

Exports in Node Js

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";
Built-in Modules

When we install Node it comes with many Built-in Modules. Here are some of the most commonly used core modules.

  • http: create HTTP servers and clients for web application development.
  • fs: This module allows you to manipulate files and directories on server.
  • path: Useful to work with directories and files paths.
  • url: Used for parsing URLs and extracting values from it

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
Node http module example

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');
Node fs module example

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);

node fs module

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:

Create Nodejs App with Visual Studio Code Editor
Learn Node application development, free node js framework tutorial with examples.
Node examples | Join JavaScript Course