当前位置:网站首页>nodejs+express realizes the access to the database mysql and displays the data on the page
nodejs+express realizes the access to the database mysql and displays the data on the page
2022-08-04 01:02:00 【m0_67392661】
1.Express基础框架的搭建
首先,创建一个websafe文件如下图所示
Cdcommand to the newly created directory.
重点步骤:
1.1初始化项目
npm init

一直往下enter键就可以

yesThere will be one more in our project laterjson文件
1.2安装express
安装命令为:
npm install -g express-generator

Just execute this command and wait for the execution to end.
1.3 Express创建一个目录,进入该目录
创建命令:
express -e websafe


1.4Download package dependencies
Download base dependencies command:
npm install
下载mysqlDatabase dependent commands:
npm install mysql
After completing the above four steps, the specific project structure is as follows:
In this case, we create an initial oneexpress项目架构
2.A connection to the database is created
前提:Built locallymysql数据库,And there is a table namedmysql
Then create a connectionjs文件(index.js),具体代码如下所示:
var express = require("express"); //Reference external module interface,That is, get the module object
var router = express.Router();//创建路由实例
//调用mysql模块
const mysql = require("mysql");
//配置本机mysqlBasic connection information
let connectInfo = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: '123456',
database: 'mysql'
})
//数据库建立连接
connectInfo.connect();
//查询sql语句
var sqlWord = 'select * from nodetest';
connectInfo.query(sqlWord, function (err, result) {
if (err) {
console.log('[query]-:' + err);
} else {
router.get('/', function (req, res, next) {
res.render('index', {
title: 'expressThe test instance connects to the database',
data: result
})
})
}
});
module.exports = router;
Remember to have it locallymysql数据库,And there is a table named in the databasenodetest
3. 页面的展示(ignore creationmysqlThe process of the local database)
The creation page is displayed through variables,通过express框架中的index.ejsto create a form for the pagedata数据的展示,具体代码如下:
<!DOCTYPE html>
<html>
<head>
<title>
<%= title %>
</title>
<!-- 引入bootstrap框架 -->
<link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<link rel='stylesheet' href='/stylesheets/index.css' />
<!-- Styles are based onpublicfrom below -->
</head>
<body>
<div class="parts">
<div class="part">
<div class="fh3">家庭信息表 <sub> (测试nodejs+express+mysql)</sub></div>
<table class="table table-bordered text-center">
<tr>
<td>id</td>
<td>name</td>
<td>address</td>
<td>user</td>
</tr>
<% for(var i=0;i<data.length;i++){ %>
<tr>
<td>
<%= data[i].id %>
</td>
<td>
<%= data[i].name %>
</td>
<td>
<%= data[i].address %>
</td>
<td>
<%= data[i].user %>
</td>
</tr>
<% } %>
</table>
</div>
</div>
</body>
</html>
The style can be freely defined by yourself,无关紧要的,需要demoYou can private me or leave an email.
After completing these operations, you will find that the final page will display the relevant data of the database,其实是采用了expressBuild the front frame,nodejs后台实现,The final page effect is shown below:
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢
随机推荐
越来越火的图数据库到底能做什么?
LDO investigation
.NET Static Code Weaving - Rougamo Release 1.1.0
【详细教程】一文参透MongoDB聚合查询
fsdbDump用法
教你如何定位不合理的SQL?并优化之
阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
Analysis of usage scenarios of mutex, read-write lock, spin lock, and atomic operation instructions xaddl and cmpxchg
outputBufferIndex = mDecode.dequeueOutputBuffer(bufferInfo, 0) 一直返回为-1
快速入门EasyX图形编程
如何用C语言代码实现商品管理系统开发
typescript52-简化泛型函数调用
jmeter分布式压测
Electronics manufacturing enterprise deployment WMS what are the benefits of warehouse management system
动态内存二
WMS仓储管理系统能解决电子行业哪些仓库管理问题
C 学生管理系统_分析
typescript51 - basic use of generics
电子制造企业部署WMS仓储管理系统的好处是什么
2023年航空航天、机械与机电工程国际会议(CAMME 2023)









