我是node.js的新手,并尝试在html网站上显示一个简单的表格.这个表应该填充来自mysql select的数据(例如:select * from testtable).但我真的不明白该怎么做.对于具有PHP背景的人,如何在nodejs中的HTML中显示某些sql...
我是node.js的新手,并尝试在html网站上显示一个简单的表格.这个表应该填充来自mysql select的数据(例如:select * from testtable).
但我真的不明白该怎么做.对于具有PHP背景的人,如何在nodejs中的HTML中显示某些sql内容?我知道git mysql nodejs wiki但它根本没用,因为这只是关于获取数据而不是在网页上显示它.
在nodeJS中做类似事情的最佳做法是什么?有一个容易理解的方式,还是我应该继续使用PHP?我真的很喜欢关于node.js的想法,但开始就像爬一个滑溜溜的悬崖.
我也买了一本关于它的书,但这本书从来没有深入到表达(因为这将是太多的信息.??…..).所以我知道express,pug,serve-static,我能够提供一个简单的html.但就是这样,没有CRUD或REST,我付了50欧元.
编辑:
我是否需要使用API??或者是否正确使用Angular.js?
EDIT2:
C:\Users\user\node_test\CRUD2>node app.js
module.js:327
throw err;
^
Error: Cannot find module './routes'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Users\user\node_test\CRUD2\app.js:2:14)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
EDIT3:
C:\Users\user\node_test\CRUD2>node app.js
Express server listening on port 4300
C:\Users\user\node_test\CRUD2\routes\testtable.js:4
var query = connection.query('SELECT * FROM testtable',function(err,rows){
^
TypeError: Cannot read property 'query' of undefined
at C:\Users\user\node_test\CRUD2\routes\testtable.js:4:29
at C:\Users\user\node_test\CRUD2\node_modules\express-myconnection\lib\express-myconnection.js:87:41
at Pool.<anonymous> (C:\Users\user\node_test\CRUD2\node_modules\mysql\lib\Pool.js:47:16)
at Handshake.Sequence.end (C:\Users\user\node_test\CRUD2\node_modules\mysql\lib\protocol\sequences\Sequence.js:78:24)
at Handshake.ErrorPacket (C:\Users\user\node_test\CRUD2\node_modules\mysql\lib\protocol\sequences\Handshake.js:101:8)
at Protocol._parsePacket (C:\Users\user\node_test\CRUD2\node_modules\mysql\lib\protocol\Protocol.js:205:24)
at Parser.write (C:\Users\user\node_test\CRUD2\node_modules\mysql\lib\protocol\Parser.js:62:12)
at Protocol.write (C:\Users\user\node_test\CRUD2\node_modules\mysql\lib\protocol\Protocol.js:37:16)
at Socket.<anonymous> (C:\Users\user\node_test\CRUD2\node_modules\mysql\lib\Connection.js:73:28)
at emitOne (events.js:77:13)
解决方法:
这里有一个例子:
app.js
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
//Including controller/dao for testtable
var testtable = require('./routes/testtable');
var app = express();
var connection = require('express-myconnection');
var mysql = require('mysql');
// all environments
app.set('port', process.env.PORT || 4300);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.use(
connection(mysql,{
host: 'localhost',
user: 'your_user',
password : 'your_password',
port : 3306, //port mysql
database:'dbname'
},'pool')
);
app.get('/testtable', testtable.list);
app.use(app.router);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
路线/ testtable.js
exports.list = function(req, res){
req.getConnection(function(err,connection){
var query = connection.query('SELECT * FROM testtable',function(err,rows){
if(err)
console.log("Error Selecting : %s ",err );
res.render('testtable',{page_title:"Test Table",data:rows});
});
});
};
意见/ testtable.ejs
<html><body>
<table border="1" cellpadding="7" cellspacing="7">
<tr>
<th width="50px">No</th>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
</tr>
<% if(data.length){
for(var i = 0;i < data.length;i++) { %>
<tr>
<td><%=(i+1)%></td>
<td><%=data[i].field1%></td>
<td><%=data[i].field2%></td>
<td><%=data[i].field3%></td>
</tr>
<% }
}else{ %>
<tr>
<td colspan="3">No user</td>
</tr>
<% } %>
</table>
</body></html>
要运行此“项目”,您必须安装一些节点模块,您可以在根目录中创建一个名为“package.json”的文件:
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"ejs": "^1.0.0",
"express": "3.5.1",
"express-myconnection": "1.0.4",
"jade": "*",
"mysql": "2.2.0"
}
}
并在您的项目根目录下调用“npm install”.
我希望它对你有所帮助.
本文标题为:在html站点上的Node.js mysql显示表
- CSS经典实用技巧18招 2022-10-16
- 初探 vite2 + vue3 2023-10-08
- echarts饼图指示器文字颜色设置不同实例详解 2022-08-30
- Vue cli写的一款PC端音乐播放器(网易云的API) 2023-10-08
- vue项目接收二进制流展示表格 2023-10-08
- 关于 css:仅在 IE7 和 IE8 中不以链接或悬停状态显 2022-09-21
- Typescript井字棋的项目实现 2022-10-21
- 纯html+css实现打字效果 2022-09-21
- JS实现表格响应式布局技巧 2022-08-30
- 史上最强vue总结来了,终获offer 2023-10-08