nodejs ejs模板数据库操作

1. 建立数据连接setting.js

var settings = {};
settings.db = {
host: 'localhost',
user: 'root',
password: '1234',
port: '3306',
database: 'testnode'
}
module. exports = settings;

 2. mysqlconn.js

var mysql = require( 'mysql');
var settings = require( './setting')
var conn = mysql. createConnection(settings.db);
conn. connect();
module. exports = conn;

3. app.js

app. set( 'views', path. join(__dirname, 'views'));
//设置模板的后缀是html
app. engine( 'html', require( 'ejs').renderFile);
//指定总模板
app. set( 'view engine', 'html');

 4.建立路由idnex.js

var express = require( 'express');
var router = express. Router();
var data = require( '../DataSql/index')
//用户学生列表
router. get( '/', function( req, res, next)
{
data. getList( function( err, retult)
{
res. render( 'index', {
title: '学生列表',
data:retult
});
})
});
module. exports = router;

5.views 建立index.html

DOCTYPE html>
< html lang= "en">
< head>
< meta charset= "UTF-8">
< title>
<%=title%>
title>
head>
< body style= " margin:auto 0">
< form action= "/users/search" method= "post">
< div style= " padding: 0fr; position:relative; margin: 0 auto;">
姓名:< input type= "text" name= "s_name" value= " < %=data.usernmae%>">
年龄:< input type= "text" name= "s_age" value= " < %=data.sex%>">
< input type= "submit" value= "查询">
div>
div>
form>
< table style= " margin: 10px">
< tr>
< th>编号th>
< th>姓名th>
< th>年龄th>
< th>操作th>
tr>
<% for(var i=0;i< data.length;i++){%>
< tr>
< td>
<%=data[i].id%>
td>
< td>
<%=data[i].usernmae%>
td>
< td>
<%=data[i].sex%>
td>
< td>< a href= "/users/add">添加用户a>td>
< td>< a href= "/users/del/ < %=data[i].id %>">删除a>td>
< td>< a href= "/users/toUpdate/ < %=data[i].id %>">修改a>td>
tr>
<%}%>
table>
body>

html>

5.数据库操作index.js

//var db = require("./mysql");
var conn = require( "../DataSql/sqlconn");
/* exports.getList = function (callback)
{
var sqlstr = "select * from student";
db.driver.execQuery(sqlstr,function(err, data)
{
return callback(err, data);
});
}; */
exports. getList = function ( callback)
{
var sqlstr = "select * from student";
conn. query(sqlstr, function( err, data)
{
return callback(err, data);
});
};
//conn.end();
//console.log(db);
pasting

 

数据库

/*
Navicat MySQL Data Transfer
Source Server         : localhost
Source Server Version : 50519
Source Host           : localhost:3306
Source Database       : testnode
Target Server Type    : MYSQL
Target Server Version : 50519
File Encoding         : 65001
Date: 2018-09-14 15:26:22
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL,
  `usernmae` varchar(255) DEFAULT NULL,
  `sex` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'dqh', '33');
INSERT INTO `student` VALUES ('2', 'ddd', '11');
INSERT INTO `student` VALUES ('3', 'eee', '11');

 

你可能感兴趣的:(nodejs ejs模板数据库操作)