express

来源:互联网 发布:萨博尼斯奥运会数据 编辑:程序博客网 时间:2024/06/05 04:59
//模拟后台let express = require ('express');let app=express();app.listen(3000);
//以下代码就是解决跨域问题app.all('*', function(req, res, next) {    res.header("Access-Control-Allow-Origin", "http://localhost:8080");//允许8080端口访问    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");//允许接收的头    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");//允许的方法    res.header("Access-Control-Allow-Credentials",' true');//允许跨域设置cookie    res.header("X-Powered-By",' 3.2.1');    if(req.method=="OPTIONS") res.send(200);/*让options请求快速返回 如果发的是options请求 响应ok 即可*/    else  next();});
//获取轮播图数据 当访问/slider是把数据返回let sliders=require('./mock/slider');app.get('/slider',function(req,res){    res.json(sliders)});

 
原创粉丝点击