如何在节点js

时间:2018-01-15 14:14:38

标签: javascript html mysql node.js

我使用node express连接到Mysql,我可以从mysql编写提取查询,但结果以数组的形式出现在控制台中,我是初学者Node.js,请帮助我如何将mysql的结果显示到HTML template

看看我有什么:

config.js:

   ....

           var express = require('express');
           var mysql = require('mysql');         
        //  get connect to mysql database
               var app = express();                      // http connection
               var bodyParser = require('body-parser');  // Handle to get interface form data

          var flash = require('connect-flash');   // enable the use of flash message 
          var session = require('express-session');
          var cookieParser = require('cookie-parser');
          var validator = require('express-validator');

     // Create routes of folder which contain libraries and functions

        var router = express.Router();
        var path    = __dirname + '/views/';
     // Create routes 

      router.use(function (req,res,next) {
      console.log("/" + req.method);
        next();
      });

    router.get("/",function(req,res){
      res.sendFile(path + "index.html");
      }); 
 app.use("/",router);

     // Fetch display books from mysql to home page 

       connection.query("SELECT * FROM books_store", function (err, result, fields) {
         if (err) throw err;
      console.log(result);
       });

  app.listen(1000, function () {
     console.log('app is listening on port 1000!');
     console.log('run "localhost:1000" in your browser!');
      });

 ....

Html代码:

index.html(我需要结果如下,但我不知道该怎么做):

  ....

  @for(results){
    <div class="post-header font-alt">
     <div class="post-meta">&nbsp;<p>@results.Bookname</p></div>
              </div>
     <div class='caption'>
    <p><span class="fa fa-bookmark"></span>@results.price</p>
</div>       
   <div class="fa fa-bar-chart"><p>@results.quantity</p></div>
  }
....

0 个答案:

没有答案