Node.js introduction and configure a test project in Eclipse

Author - Preeti Bisht
20.04.2018
|
0 Comments
|

Node.js is an open-source, cross-platform runtime environment used for development of server side applications and networking applications.

Basic features of Node.js are :

1. Most of its modules are written in JavaScript.
2. It is mostly used in real time server applications as it is based on event-driven architecture and non-blocking API which optimises the scalability and throughput of an application.
3. It has provided a way for web applications to have a real-time, two-way connection allowing both clients and server to initiate communication to exchange data, which is different from the stateless model where data is maintained only within a session.
4. Node.js uses a single threaded program and the same program can provide service to a much larger number of requests than traditional server like Apache HTTP server.
5. It also provides an environment that allows including so many supporting tools and different packages to make it a full-fledged server implementation environment.
6. Node.js is not recommended to be used in the applications requiring long processing time as it uses single threaded programs so while one request with long processing time is processing, it won’t be able to process any other request.

Steps to setup node.js project in eclipse:

we can download nodejs eclipse IDE from http://www.nodeclipse.org/
or nodejs plugin for already installed eclipse by following these steps:

1. First Download and install Node.js from https://nodejs.org/
or run the following command:

sudo apt-get install nodejs

(you must have JDK in your machine before downloading it)
2. Go to Help toolbar and click eclipse marketplace.
3. Search for node.js in marketplace from search bar.
4. From there you can install nodejs eclipse IDE.
5. Now to create nodejs project , Go to file->New Project->choose node.js project and create the project.
6. To run node.js project, click on run as and choose Node Application.
7. To run it as node Application, you will need npm (NPM is a package manager for Node.js packages).
8. Install npm by right-clicking on the created project, click on debug as->install npm – it will install npm.
9. Or you can download it with this command:

sudo apt-get install npm

Database connection can be created in a nodejs project as:

// for database connection
var connection=mysql.createConnection({
host:’localhost’,
user:’root’,
password:’1234556’,
database:’Test’
});
connection.connect(function(error){
if(!error){
console.log(‘Cannot connect to database’);}
else{
console.log(‘connection Established’);
}
})

Example of User registration and login form:

Loginform
<form action=”/loginForm” method=”POST”>
<input type=”email” name=”email”>\
<input type=”password” name=”password”>
<input type=”submit” value=”Login”>

Specify routes of files:

app.get(‘/loginForm’,function(req,resp){
resp.sendFile(‘./files/index.html’,{root:_dirname});
});
app.post(‘/loginForm’,function(req,resp){
var emailId=req.body.email;
var password =req.body.password;
connection.query(select email,password from users where email=?,[emailId])
if(error)
{
res.send({
“code”:500,
“Error Message”:”Some error occured”
})
}
else
{
if(rows[0].password==password)
{
res.send({
“code”:200,
“Success Message”:”Login successful”
})
}
}
)};

For Registration Form:

app.post(‘/registrationForm’,function(req,resp){
var userData={
“name”:req.body.name,
“email”:req.body.email,
“Password”:req.body.password
}

connection.query(‘insert into users set ?’,userData,function(error,rows,field)
{
if(error)
{
res.send({
“code”:500,
“Error Message”:”Some error occured”
})
}
else
{
res.send({
“code”:200,
“Success Message”:”user registered successful”
})
}
)};

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *