美文网首页
node发送http请求,跨域, nodemon自动重启服务

node发送http请求,跨域, nodemon自动重启服务

作者: 七彩霞光_d533 | 来源:发表于2019-06-04 14:17 被阅读0次
配置nodemon

转自https://blog.csdn.net/luchuanqi67/article/details/81304352

  1. npm install -g nodemon
  2. 在项目根目录创建 nodemon.json 文件
{
  "restartable": "rs",
  "ignore": [
    ".git",
    ".svn",
    "node_modules/**/node_modules"
  ],
  "verbose": true,
  "execMap": {
    "js": "node --harmony"
  },
  "watch": [
    
  ],
  "env": {
    "NODE_ENV": "development"
  },
  "ext": "js json njk css js "
}
  1. 配置 npm run dev 命令
    package.json 的 scripts 中添加 "dev": "nodemon app" (app.js 为根目录文件)
    用 npm run dev 启动项目
{
  "name": "demo",
  "version": "0.0.0",
  "scripts": {
    "start": "node app",
    "dev": "nodemon app"
  }
}
express 跨域请求
app.all(' * ', (req , res)= >{
        res.header("Access-Control-Allow-Origin", "*");
        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");
})

koa2 跨域请求
const  cors = require('koa2 - cors ')

server.use(cors({
  origin: function (ctx) {
      if (ctx.url === '/system') {
          return "*"; // 允许来自所有域名请求
      }
      return 'http://localhost:3000'; // 这样就能只允许 http://localhost:8080 这个域名的请求了
  },
  exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
  maxAge: 5,
  credentials: true,
  allowMethods: ['GET', 'POST', 'DELETE'],
  allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}))

请求
const https = require('https');  
const http = require('http');
const qs = require('querystring');  
const url = require("url");

get 请求
var access_data = {  
        access_token,  
    } 
    var access_content = qs.stringify(access_data);  
    var access_options = {
        hostname: 'api.weixin.qq.com',  
        port: '',  
        path: '/cgi-bin/message/wxopen/activityid/create?' + access_content,  
        method: 'GET' ,
        header: {
            "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
        }, 
    };  

    var _req = https.request(access_options, function (_res) {  
            
        _res.on('data', function (_data) {  

              let reault_data = JSON.parse(_data.toString())
              res.send({result:1,msg:'ok',reault_data ,}).end();
              
          });  
          
      });  

      _req.on('error', function (e) {  
        console.log('获取消息失败 ' + e);  
      });  
  
      _req.end();
post 请求
var update_data = {  
            access_token,
            activity_id,
            target_state:1,
        } 
        // var update_content = qs.stringify(update_data);  
        var update_content = JSON.stringify(update_data); 
        let uri = url.parse('https://api.weixin.qq.com/cgi-bin/message/wxopen/updatablemsg/send?access_token='+access_token)
        var update_options = {
            method: "POST",
            hostname: uri.hostname,  
            port: '',  
            path: uri.path,  
            headers: {
                // "Content-Type": "application/x-www-form-urlencoded",
                "Content-Type": "application/json", 
                "Content-Length": update_content.length
            }, 
        };  
    
        var _req = https.request(update_options, function (_res) {  
            _res.on('data', function (_data) {  
                  let reault_data = JSON.parse(_data.toString())
            });  
              
          }); 
          
          _req.on('error', function (e) {  
            console.log('更改消息失败 ' + e);  
          });  
          _req.write(update_content);
          _req.end();


相关文章

网友评论

      本文标题:node发送http请求,跨域, nodemon自动重启服务

      本文链接:https://www.haomeiwen.com/subject/srfmtctx.html