Nodejs笔记整理05

一 连接数据库

npm install mysql 安装连接数据库依赖包

    //mysqlpool文件夹内容
    var mysql = require('mysql')
    var pool =mysql.createPool({
        connectionLimit:10,//最大连接数
        host:"localhost",
        user:'root',
        password:'',
        database:"ceshi"
    })
    module.exports = pool;//连接池
// 路由文件
var express = require('express');
var router = express.Router();
var pool = require('../dao/mysqlpool');//导入数据库连接的文件
router.get('/', function (req, res, next) {//router.get('/')是二级路由下面的路径,app.js是一级目录下的
  pool.getConnection(function (err, connection) {
    if (err) console.log(err); 
    let getuser = "select * from login";//从哪个表里查询
    connection.query(getuser, function (error, result) {
      if (error) console.log(error);
      res.render('admin', { data: result });//第一个参数是模板ejs,jade模板目录下的文件{第二个参数}
    });
  });
});
module.exports = router;
//app.js 文件
var usersRouter = require('./routes/users');
app.use('/users', usersRouter);

二 api接口书写规范

api 设计规范
购物车功能
数据操作
CRUD(下面四种的简写,公司里常用的)
Create
Read
Update
Delete

接口命名,比较早的一种形式
增 http://localhost:8080/shopcar/add
删 http://localhost:8080/shopcar/remove
改 http://localhost:8080/shopcar/update
查 http://localhost:8080/shopcar/search

三 新的接口命名规范 restful api

4个操作 对应的是同一个模块
可以使用不同的请求方式来进行区分

增 http://localhost:8080/shopcar post请求
删 http://localhost:8080/shopcar delete请求
改 http://localhost:8080/shopcar put请求
查 http://localhost:8080/shopcar get请求

express 框架 完全支持restful api

const router = express.Router();
并且 路由 route()函数 支持链式调用

router
    .route('/shopcar')
    .get((req, res, next) => {
        res.json({
            msg: "查询操作"
        });
    })

四 前端渲染和后端渲染

前端渲染
前端向后端发送数据请求 获得数据结果后使用DOM操作渲染页面

后端渲染
用户向后端发送请求 后端通过模板引擎将数据拼接成html 并返回给用户

五 模板引擎

模板引擎 就是将数据 和 html进行结合 生成一个html文件 给用户
express 默认构建的项目 采用的是jade模板

express myapp 默认使用jade/pug,myapp是自己想要构建的项目名
express myapp -e -e表示使用ejs 模板

jade/pug 语法相同 一模一样ja de因版权问题更名为pug

jade因版权问题更名为pug
jade 代码结构依赖缩进
标签名 没有尖括号 没有闭合标记
标签内容写在标签名后 用空格分隔
属性 写在标签名后 使用div(class=“box” id=“content”)
div(class=“box”) hello world
div.box hello world
div.box#content hello world
.box#content hello world
h1.title 你好加类名写法
div=变量
div #{变量}内容

六 jade参考语法

https://blog.csdn.net/mytljp/article/details/79133725
html转jade
http://html2jade.org/

参考示例
https://github.com/jxsrzj0325/blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值