Node+express 开发api服务器记录

Node开发记录

图片验证码解决方案:

svg-captcha中间件。

用户登录注册验证解决方案:

1、使用passport中间件。
参考连接:
https://www.cnblogs.com/y-yxh/p/5859937.html

目前只考虑本地策略,即用户名和密码,第三方qq、微信等登录方式暂不考虑。
需要依赖:npm install --save passport passport-local

2、使用jsonwebtoken中间件来签发token
参考连接:https://blog.csdn.net/u010180452/article/details/93981568

3、使用express-jwt验证token
参考连接:https://blog.csdn.net/qq_27818541/article/details/76656784
https://www.cnblogs.com/zkqiang/p/11810203.html

4、用户退出,客户端抛弃token就可以了。服务端不需要进行token操作。

图片文件上传功能

使用multer中间件
npm install --save multer
或是使用formidable 中间件,参考地址https://segmentfault.com/a/1190000015558975

前后台跨域问题解决方案:

1、cors中间件直接解决跨域请求(开发和生成环境,都可以的,真正的跨域)。【生成环境使用】
安装npm install cors --save
然后在app.js使用

app.use(cors({
  origin:['http://localhost:8081'],
  methods:['GET','POST'],  //指定接收的请求类型
  credentials: true,
  alloweHeaders:['Content-Type','Authorization']  //指定header
}))

2、vue-cli前端配置跨域代理(开发环境代理的形式跨域)【开发环境使用】
以cli@3为例:

// vue.config.js
module.exports = {
  devServer: {
    proxy: {
      "/api": {
        target: "http://127.0.0.1:3000/",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/api": ""
        }
      }
    }
  }
};
// .env.development
VUE_APP_BASE_API=/api
// axios实例配置
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 60 * 1000 // 请求超时时间设置
});

前端角色路由权限解决

参考:https://www.jianshu.com/p/78dac627d9ea【vue+axios 实现登录拦截权限验证】

原创文章 103 获赞 128 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_42991509/article/details/103709119