TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
jigar patel
NA
310
23.9k
connect SQL in Node js
Dec 13 2018 5:55 AM
var
express = require(
'express'
);
var
bodyParser = require(
"body-parser"
);
var
app = express();
var
sql = require(
"mssql"
);
// Body Parser Middleware
app.use(bodyParser.json());
//CORS Middleware
app.use(
function
(req, res, next) {
//Enabling CORS
res.header(
"Access-Control-Allow-Origin"
,
"*"
);
res.header(
"Access-Control-Allow-Methods"
,
"GET,HEAD,OPTIONS,POST,PUT"
);
res.header(
"Access-Control-Allow-Headers"
,
"Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization"
);
next();
});
// config for your database
var
config = {
user:
'sa'
,
password:
'abc@123'
,
server:
'MY-PC\\SQL'
,
database:
'dbStudent'
};
sql.close()
app.get(
'/GetTotalStudent'
,
function
(req, res) {
// connect to your database
sql.connect(config,
function
(err) {
if
(err) console.log(err);
// create Request object
var
request =
new
sql.Request();
// query to the database and get the records
request.query(
'select count(*) as totalrow from tblStudent'
,
function
(err, recordset) {
if
(err) console.log(err)
// send records as a response
res.send(recordset.recordset[0]);
sql.close();
});
});
});
app.get(
'/GetCurrentData'
,
function
(req, res) {
// connect to your database
sql.connect(config,
function
(err) {
if
(err) console.log(err);
var
startpage = parseInt(req.query.Start);
var
endpage = parseInt(req.query.End);
// create Request object
var
request =
new
sql.Request();
var
query =
" SELECT * FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY st_Id ) AS RowNum, * "
+
" FROM tblStudent "
+
" ) AS RowConstrainedResult "
+
" WHERE RowNum >= "
+ startpage +
" "
+
" AND RowNum <= "
+ endpage +
" "
+
" ORDER BY RowNum"
;
// query to the database and get the records
request.query(query,
function
(err, recordset) {
if
(err) console.log(err)
// send records as a response
res.send(recordset.recordsets);
sql.close();
});
});
});
//POST API
app.post(
"/addemployee"
,
function
(req , res){
var
query =
"INSERT INTO [tblEmployee] (firstname,lastname,email,password,birthdate,phonenumber,height,weight,gender) VALUES ('"
+req.body.firstname+
"','"
+req.body.lastname+
"','"
+req.body.email+
"','"
+req.body.password+
"','"
+req.body.birthdate+
"','"
+req.body.phonenumber+
"','"
+req.body.height+
"','"
+req.body.weight+
"','"
+req.body.gender+
"')"
executeQuery (res, query);
});
var
server = app.listen(9090,
function
() {
console.log(
'Server is running..'
);
});
Reply
Answers (
1
)
What is $evalAsync
add push message notification in angular js application