Answer the question
In order to leave comments, you need to log in
Nodejs Express how to display required query result in html?
Good afternoon!
I am writing a web server from scratch, which, based on the results of a post request, should produce a result on the page.
To do this, I installed nodejs, connected express, express-generator, and a module for communicating with the MSSQL database, mssql.
Through the generator ( expressjs.com/starter/generator.html ) I created the application layout.
In the model, I created the db.js file for accessing the database and executing a stored procedure with parameters.
var sqlDb = require('../node_modules/mssql');
var settings = require('./settings');
exports.selectOubySummBuking = function(mon, tue, wed, thu, fri, sat, sun, buksumm, sql, callback){
var conn = new sqlDb.Connection(settings.dbConfig);
conn.connect()
.then(function(){
var req = new sqlDb.Request(conn);
//req.verbose = true;
req.input('mon', mon);
req.input('tue', tue);
req.input('wed', wed);
req.input('thu', thu);
req.input('fri', fri);
req.input('sat', sat);
req.input('sun', sun);
req.input('buksumm', buksumm);
req.execute(sql)
.then(function(data){
callback(data[0]);
})
.catch(function(err){
console.log(err);
callback(err);
});
})
.catch(function(err){
console.log(err);
callback(err);
});
};
var express = require('express');
var dbconnect = require('../model/db.js');
var outlets = require('../controller/outlet.js');
var router = express.Router();
// определяем метод который будет обрабатывать обращение
router.post('/oudb', function(req, res){
var allBuking = req.body.allBuking;
dbconnect.selectOubySummBuking(2, 2, 3, 4, 5, 5, 3, allBuking, "selectOubySummBuking", function(data) {
res.render('index', {
title: "Outlet DB",
results: data
});
});
});
module.exports = router;
<!DOCTYPE html>
<html lang = "ru">
<head>
<meta charset="utf-8">
<title>My app</title>
<link rel="stylesheet" href="bootstrap/dist/css/bootstrap.css">
<script src = "jquery/dist/jquery.js"></script>
<script src = "bootstrap/dist/js/bootstrap.js"></script>
</head>
<body>
<form action="/oudb" method="post">
<input name="allBuking" type="number">
<input type="submit">
</form>
<ul>
<% for(var i=0; i<results.length; i++) {%>
<li><%= results[i].City %></li>
<% } %>
</ul>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question