loading...

Node.JS rest api Tutorials

sman40901 profile image sman40901 ・2 min read

Install appropriate node version from here :
https://nodejs.org/en/download/

All these commands must be run on command prompt after npm package installation.
generate an empty npm project without going through an interactive process.
npm init -y

install required (driver) packages
npm i express mysql body-parser cors

install nodemon, it is a tool that helps by automatically restarting the node
application when file changes in the directory are detected
npm i -g nodemon

//First create a folder (lers say "nodejs-rest-api")
// and then create a file in that folder called "app.js"
//and you should add this code :

var express = require("express");
var app = express();
// mysql driver
var mysql = require("mysql");
// parser to parse POST body content
var bodyParser = require("body-parser");
// this is the port where your application will run (eg: localhost:9800)
var port = 9800;

// create connection object for database
let connection = mysql.createConnection({
host: "localhost",
user: "user",
password: "password",
database: "somedatabase",
});

app.use(bodyParser.json()); // so it can support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
// so it can support URL-encoded bodies
extended: true,
})
);

// this will connect MySQL
connection.connect();

// this will create a get request with route "localhost:9800/"
// req -> request
// res -> response
app.get("/", (req, res) => {
res.send("Application started");
});

// this will create a get request with route "localhost:9800/coaches"
app.get("/coaches", (req, res) => {
connection.query("SELECT * FROM coachmain;", (err, result) => {
if (err) {
console.log(err);
res.json({ error: true });
} else {
console.log(result);
res.json(result);
}
});
});

// this will create a delete request with route "localhost:9800/deleteCoach"
/*
delete body should be something like this :
{
"coachId": 1
}
*/
app.delete("/deleteCoach", function (req, res) {
// get data from forms and add to the table called coachmain
var coachId = parseInt(req.body.coachId);
var queryString =
DELETE FROM coachmain
where coachId =
'
+
coachId +
';
connection.query(queryString, function (err, result) {
if (err) {
// handle error and notify user
res.status(400).send(err);
} else {
// success message here. if you forget this,
// the request will wait till timeout
res.status(201).send(req.body);
}
});
});

// this will create a post request with route "localhost:9800/deleteCoach"
/*
post body should be something like this :
{
"name": "full name",
"coachId": 1,
"season": 2
}
*/
app.post("/newCoach", function (req, res) {
// get data from forms and add to the table called user..
var name = req.body.name;
var coachId = parseInt(req.body.coachId);
var season = parseInt(req.body.season);
var queryString =
INSERT INTO coachmain
(
name, coachId, season
)
VALUES
(
'
+
name +
',' +
coachId +
',' +
season +
'
)
;
connection.query(queryString, function (err, result) {
if (err) {
// handle error and notify user
res.status(400).send(err);
} else {
// success message here. if you forget this,
// the request will wait till timeout
res.status(201).send(req.body);
}
});
});

// application will listen to this port
app.listen(port, (err) => {
console.log("running on :" + port);
});

Posted on by:

Discussion

markdown guide