node發(fā)送http請(qǐng)求,跨域, nodemon自動(dòng)重啟服務(wù)

配置nodemon

轉(zhuǎn)自https://blog.csdn.net/luchuanqi67/article/details/81304352

  1. npm install -g nodemon
  2. 在項(xiàng)目根目錄創(chuàng)建 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 啟動(dòng)項(xiàng)目
{
  "name": "demo",
  "version": "0.0.0",
  "scripts": {
    "start": "node app",
    "dev": "nodemon app"
  }
}
express 跨域請(qǐng)求
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 跨域請(qǐng)求
const  cors = require('koa2 - cors ')

server.use(cors({
  origin: function (ctx) {
      if (ctx.url === '/system') {
          return "*"; // 允許來自所有域名請(qǐng)求
      }
      return 'http://localhost:3000'; // 這樣就能只允許 http://localhost:8080 這個(gè)域名的請(qǐng)求了
  },
  exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
  maxAge: 5,
  credentials: true,
  allowMethods: ['GET', 'POST', 'DELETE'],
  allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}))

請(qǐng)求
const https = require('https');  
const http = require('http');
const qs = require('querystring');  
const url = require("url");

get 請(qǐng)求
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 請(qǐng)求
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();


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容