Category: nodejs

Ubuntu Apache add reverse proxy to avoid port number visibility | NodeJS

When we create APIs using nodejs we often enable them to run on particular port number. It is all fine until we move into production environment where we don’t want our port number to visible to our clients while we are making the server request calls using the APIs.

So a solution would be to mask our port number and let apache handle the request and redirect the request to that server internally without revealing the port number. For this we need to edit the apache configuration like following.

Open the file /etc/apache2/sites-enabled/000-default.conf using any editor and add the following lines under or above the directory tag.

<Location /query-api-0.3.1>
ProxyPass http://localhost:3099/query-api-0.3.1
ProxyPassReverse http://localhost:3099/query-api-0.3.1
Order allow,deny
Allow from all
</Location>

Reverse Proxy exmaple in apache

Then save the file and restart apache and try to run the API request without the port number.

How to kill a node server using its pid(process id) that is running in background

Often we run node servers in background. Like using screen or using a cron job that starts while booting. So how do we kill it when when its running in the background.

Well, one way is if it is running in a screen we can resume the screen and terminate it.

Or even better we can find the process id of the server you are running. For this we need to remember the port on which you are running the node server.

For example if the server you want to terminate is running on the port 3029, run the below command to get the process id of that process.

lsof -i:8044

In the above command 8044 can be any port number. When we run the command we get output like below

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 108597 nagaraju 20u IPv6 318585 0t0 TCP *:8044 (LISTEN)

Notice the PID. That is what we need to terminate the process.

Now we run the below command to kill/terminate that process.

kill -9 108597

Using kill command to terminate the process and then checking if the process is still running after terminating it

Receiving a request with multiple file fields in NodeJS using multer

Multer is a handy module that enables the node API to upload files at ease. We can upload an array of files or files in separate fields or a single file using multer.

Let’s see how one can upload files in received in separate fields.

In the following example, we are going to develop a request to upload a resume and a profile photo with field names ‘resume’ and ‘profile_pic’ respectively.

Receiving Request

var express = require('express');
var multer = require('multer');
var upload = multer({
    dest: 'upload_folder'
});
var app = express();

var upload_files = upload.fields([{
    name: 'resume',
    maxCount: 1
}, {
    name: 'profile_pic',
    maxCount: 8
}]);

app.post('/upload-profile', upload_files, 
     function(req, res, next) {
    console.dir(req.files); //prints the array of uploaded files

});

app.listen(parseInt(3200));

The following code is to be placed in your localhost(html folder):

Sending Request

<form action="http://localhost:3200/upload-profile" method="post" 
      enctype="multipart/form-data">
    <div>

        <label>Upload Resume</label> <input type="file" 
          name="resume" />

        <label>Upload profile pic</label> <input type="file" 
                    name="profile_pic" />

        <input type="submit" value="Submit">

    </div>

</form>

Hope this helps! Happy coding!

Developing a POST request in nodejs

Unlike GET request which sends the data through URL may not be considered the right option in case of sending a confidential data, POST request is considered the best option as it encrypts the data in the body to send it.

Nodejs is a one of a kind where you can easily create a POST request. Let’s learn how to create a POST request.

1. We will create a request to get the user name. Let’s name the server file as server.js

server.js

var express = require('express');

var app = express();

var bodyParser = require('body-parser');


// Create application/x-www-form-urlencoded parser

var urlencodedParser = bodyParser.urlencoded({ extended: false })


//The / here represents the path to an interface to send the user name from

    app.get('/', function (req, res) {

        res.sendFile( __dirname + "/" + "index.html" );

    })


app.post('/listUsers', urlencodedParser, function (req, res) {

    // Prepare output in JSON format

    response = {

        first_name:req.body.first_name,

    last_name:req.body.last_name

    };

    console.log(response);

    res.end(JSON.stringify(response));

})


app.listen('8081');

console.log("server is up and running..");

The express module enables you to create a server while the body parser allows you to parse the body in which the user name is sent. Since we need to encode the body, the urlencoded does the job for us.

The post request uses headers to send the data, you may need different types of headers depending upon your requirement(for instance, multipart-form-data to send files and x-www-form-urlencoded to send form data). For more info about headers, click here

req.body refers to the content of the body. If you’d like to fetch specific contents from the body, you may add it at the end of req.body as in the above code where we’re fetching first_name using req.body.first_name.

2. To send a request, we first create an interface where we can send the username from.

index.html

<html>

<body>

<form action = "http://localhost:8081/listUsers" method = "POST">

 First Name: <input type = "text" name = "first_name">&nbsp; <br>

 Last Name: <input type = "text" name = "last_name">

 <input type = "submit" value = "Submit">

</form>

</body>

3. Running the server

Now that we are ready with server and interface, let’s run them.

To run the node server, type the following command(presuming you have nodejs installed or you may install it from here)

nodeserver.js

Output

server is up and running..

4. To send a request using the interface, open http:localhost:8081 in your browser and you will be redirected to webpage where you can input username


4. Click on “Submit“, which sends a request to the server and you’ll get response that returns the username

If you notice the url in the output, you don’t find the user name unlike in GET request you’ll find the user name in the URL itself. The user name in POST request in embedded in the body which is why it doesn’t appear in the URL. Thus, POST request is considered to be the best practice while sending confidential data.

Here we are representing them in JSON format. Similarly, we may save it to the file or database which I’ll demonstrate in the upcoming posts.

Happy coding! 😉

Developing a GET request to pass username using nodejs

Nodejs is a single threaded server-side platform which has gained it’s unique niche due it’s asynchronous feature thereby enhancing the speed of execution and efficiency.

The unique feature of nodejs is that you can create request and server for many web applications.

Getting Started:

1. Let’s start of with creating a basic GET request using nodejs. We will request for a username through GET request which is provided by the user.

a) Developing a GET request on server to  receive and print the username

server.js

var express = require('express');
var app = express();

app.get('/', function (req, res) {
res.sendFile( __dirname + "/" + "index.html" );
})

app.get('/getUsers', function (req, res) {
// Prepare output in JSON format
response = {
first_name : req.query.first_name,
last_name : req.query.last_name
};
console.log(response);
res.end(JSON.stringify(response));
})

app.listen('8081');
console.log("server is up and running..");

Express module helps you create a request and listen function binds the ip to the server.

b) To send a request, we first create an interface where we can input the username.

index.html

<html>
<body>
<form action = "http://localhost:8081/getUsers" method = "GET">
 First Name: <input type = "text" name = "first_name">&nbsp; <br>
 Last Name: <input type = "text" name = "last_name">
 <input type = "submit" value = "Submit">
</form>
</body>

2. Running the server

Now that we are ready with server and interface, let’s run them.

To run the node server, type the following command(presuming you have nodejs installed or you may install it from here)

node server.js

Output

server is up and running..

3. To send a request using the interface, open http:localhost:8081 in your browser and you will be redirected to webpage where you can input username

4. Click on “Submit”, which sends a request to the server and you’ll get response that returns the username.

Here we are just printing the user name sent by the user. Similarly, we may save it to the file or database.

Hope this helped you!

Happy learning!

Developing a basic responsive Telegram bot that echoes your input

Well it’s not quite easy for a beginner to develop telegram bot in a short time by exploring the telegram bot API. I’ll just brief you the procedure by developing a basic telegram bot and I’m sure this tutorial would help you kick start with developing a telegram bot of your own with in a short time.

Once you get familiar with basic bot, you could explore the official telegram bot API for customizing your bot.

Getting Started:

Let’s get started with developing a basic telegram bot.

1. Firstly, you would need to create or register your bot with Telegram. A Telegram’s bot called “BotFather” will help you do this.

Login into telegram either through web or app and search for “BotFather” in search box.

2. Select BotFather and you’ll find options to create a new bot in botfather chat.

Bot’s Name:

3.  Click on “/newbot” option and enter your bot’s name. I am naming my bot as “echo bot”.

Token:

4. Once you are done with creating your bot, you’ll be issued a token. Save this token for later use.

5. Now your bot is ready to show up on telegram, you may search your bot using it’s user name in search box.

6. Now that the bot is ready, let’s add some life to it. For this, we’ll have to develop a server for our bot to help it handle the actions(echo user input in our case). To get our server communicated with this bot, we use the token(in step 3) issued by BotFather.

Script:

7. Let’s develop a script which upon sending an input from our bot echoes it back to bot. I am using nodejs as server side language, it can also be developed in python. Since, node has pretty good number of modules that would make it easy for developing server-side, I’m using it. Here, I’m using a node module called node-telegram-bot-api which has pre-defined packages required for telegram bot API.

Server code for bot with filename echobot.js:

const http = require('http');

const https = require('https');

var TelegramBot = require('node-telegram-bot-api');

var token = 'XXXX'; //Token issued by BotFather

var bot = new TelegramBot(token, {

    polling: true

});


//getMe() returns the user name of the bot


bot.getMe().then(function(me) {

    console.log('Hi my name is %s!', me.username); //prints on console  when you run the server.

});


//on entering any text, we're converting the character case to lower


bot.onText(/.*/, function(msg, match) {

    var text = msg.text;

    text = text.toLowerCase();


    //remove special characters in the input


    text = text.replace(/[.,/#!$%^&*;:{}=-_`~()?'"@|+<>[]]/g, "");

    text = text.replace(/s{2,}/g, " ");


    //just to add AI, on input hi/hello, it prints this message


    var fromId = msg.from.id; // get the id, of who is sending the message

    if (text == "hi" || text == "hello") {

        //  console.log("hi d d gdg d"+msg.text);

        var from = msg.from.first_name; // get the id, of who is sending the message

        var message = "Hi " + from + " Welcome to your eng-hin Botn"

        message += "Type any english word and get the respective hindi meaning.";

        bot.sendMessage(fromId, message);

    } else {

        message = text;

        bot.sendMessage(fromId, message); //echoes the message

    }


});

});

8. Now that our server is ready, let’s run the server using the following command:

$ node echobot.js

9. Try the echo bot in telegram by sending the messages.

That’s it, your bot is ready to talk to you now 🙂 Hope you are aware of customizing your own telegram bot now.

Happy coding 😉

Where do I learn to code bots?

If you are well versed with API and has some hands-on experience with server-side programming ilke nodejs or php or python , then you could refer Messenger platform guide to build messenger bots.
If you are a newbie to programming, then using a bot engines would be a better option. Once you are familiar with messenger bot, you will be able to build bots on other platforms like telegram, slack etc.

I have listed few bot engines which makes it easy for you to build a bot.

wit.ai

gupshup bot builder

Chatfuel

Flowxo

API.ai

Botsify

Smooch

Botkit

I have built a few bots in messenger. Let me share them if that might help.

e Bhasha Dictionary bot

This is an English-Hindi dictionary bot. Enter any English word and it gives you the corresponding Hindi meaning.

e Bhasha News bot

This bot gives you the daily news updates, you can also subscribe to daily news alerts. This is available in 4 Indian languages(Hindi, Urdu, Punjabi, Telugu).

e Bhasha Quiz bot

This is a quiz bot that shoots general knowledge questions and is available in 3 Indian Languages(English, Telugu, Hindi ).

e Bhasha Translation bot

This is a translation bot that helps you translate any words or sentences in regional languages. Currently, this bot supports Hindi-Punjabi, Hindi-Urdu, English-Hindi language pairs.

We look forward to building many more bots as such for regional language support to help people eliminate the language barrier.

Happy Learning!

How to build a basic facebook messenger bot using Nodejs ?

To build a facebook bot, you may choose any server side technology like php, nodejs, python, ruby. I am choosing Nodejs, as it is simple and has built in libraries that would ease in building a bot.

To start building a bot, you need to install Nodejs on your computer. You may install nodejs from link below:

https://nodejs.org/en/download/

Let’s start of  with building that would say hello on your input.

step 1:

The front end API is provided by facebook which can be accessed through a facebook page. To accomplish this you’ll have to create a facebook page.

step 2:

In order to have your bot communicate with the page, facebook provides a unique token which is to be included in your code. To generate a token, you will have to create a facebook app here. Once you are done creating the app, click on “Messenger” and select your page from the drop down which generates a random token. Have this token saved for later use in the code. Then click on “Add Products” and select webhooks.

step 3:

Now that we have token ready, lets write the code say file name is server.js. Note that the above generated token needs to be replaced in the below code 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    const http = require('http')
    const Bot = require('messenger-bot')
    var bot = new Bot({
     token: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
     verify: 'VERIFY_TOKEN'
    })
    bot.on('error', (err) => {
     console.log(err.message)
    })
    bot.on('message', (payload, reply) => {
     var text = payload.message.text;
     text = text.toLowerCase();
     text = text.replace(/[.,/#!$%^&*;:{}=-_`~()?'"@|+<>[]]/g,"");
     text = text.replace(/s{2,}/g," ");
     console.log(text);
     if(typeof text === "undefined" || text == "")
     {
     text = "Oops! we couldn't find this word in our database. Try other.";
     console.log("Sorry! we couldn't find this word in our database. Try other.");
     }
    else if(text == "hi" || text == "hello"){
     text= "Hi. I am hello bot.";
     reply({ text }, (err, info) => {
     });
    else if(text == "bye"){
     text= "Bye. See ya!.";
     reply({ text }, (err, info) => {
     });
     else if(text == "How are you?"){
     text= "Am fine thanks.";
     reply({ text }, (err, info) => {
     });
     bot.getProfile(payload.sender.id, (err, profile) => {
      if (err) throw err
      reply({ text }, (err) => {
       if (err) throw err
       console.log(`Echoed back to ${profile.first_name} ${profile.last_name}: ${text}`)
      })
     })
    });
    http.createServer(bot.middleware()).listen(3000);
    console.log("chat bot up and running..");
step 4:

Now that we are done with the code, its time to run our bot. To run the server, type node server.js.

step 5:

Since, the facebook API communicates through secure tunnel, we need a secure server to establish the connection between facebook and our node server. Ngrok comes to the rescue in this case. Ngrok provides a secure tunnel which allows our server to connect to facebook API.  Install and run Ngrok which generates a secure link. Copy the generated secure link(looks like https://localhost:XXX).

step 6:

In the facebook Developers page, click on “Webhooks” and paste the above secure link in callback url field and enter the verify token from code in “Verify Token” section, click “Verify and save”. To activate the tunnel, click on “Messenger”  and select your page and click on “Subscribe” to subscribe it.

step 7:

Finally, we are done with setup and our bot is ready to talk. In the created facebook page, click on Message and type “Hi” and boom you’ll get the response.

This is a basic chat bot which simply sends text message. Similarly, you can use different templates available in Messenger platform to make your own bot.