本demo是使用express+mySql做一个简单的链接sql工程,准备安装node.jshttp://nodejs.cn/installexpresshttp://www.expressjs.com.cn/installMysqlhttps://万维网。mysql.com/项目结构初始化第一步:首先新建一个快递站点。这些新项目的名称是expresssql。如果不知道express的使用说明,可以通过express–help查看(插一句,在此之前确保express已安装)第二步:进入项目目录cdsql第三步:安装依赖npminstall第四步:启动这个项目*&npmstart,如果你安装了pm2,也可以使用pm2来启动第一个Step5:命令行会提示监听哪个端口。如果要更改默认端口号,请修改bin文件夹下的www文件。第六步:打开浏览器查看localhost:3000(默认)。express生成的http://www.expressjs.com.cn/首页+功能实现写在routes文件夹下的item.js中,用于系统和用户相关的路由配置注意:在app.js文件中引入路线中的item.js,varitem=require('./routes/item');并使用app.use()将应用程序挂载到应用程序上app.use('/item',itemRouter);所以在访问item的路径时,会变成/users/item新建一个html页面
现在在你电脑的http://localhost:3000/routes文件夹下的item.js下输入注册的路由varexpress=require('express');//引入express模块??varrouter=express.Router();//路由varmysql=require('mysql');//引入mysql模块varconnection=mysql.createConnection({//链接数据库池host:'localhost',user:'root',//用户名密码:'123456'//配置mysql时设置密码});router.post('/list',function(req,res,next){res.header('Access-Control-Allow-Origin','*');connection.query('SELECT*FROMbaobei.list_table',function(err,rows,fields){res.send(rows)});})router.post('/detail',function(req,res,next){varcontent=req.body.arr;re??s.header('访问控制允许来源','*');connection.query("SELECT*FROMbaobei.list_tableWHEREid='"+content+"';",function(err,rows,fields){res.send(rows);});});module.exports=router;