Node.js学习笔记-https与跨域问题

一、Node.js知识点

1.Node.js配置https

var express = require(“express”);
var app = express();       //使用nodejs自带的http、https模块
var https = require('https');
var http = require('http');
var fs = require('fs');    //用于读入证书文件
var path = require("path");
//根据项目的路径导入生成的证书文件 
var privateKey  = fs.readFileSync(path.join(__dirname, '/public/215028913200048.key'), 'utf8');
var certificate = fs.readFileSync(path.join(__dirname, '/public/215028913200048.pem'), 'utf8');
var credentials = {key: privateKey, cert: certificate};
//创建http与HTTPS服务器
var httpServer = http.createServer(app,function () {
    console.log('HTTP Server is running on: http://localhost:%s', PORT);
});
httpServer.listen(3000);
//创建http与HTTPS服务器
var httpsServer = https.createServer(credentials, app,function () {
    console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT); });  //可以根据请求判断是http还是https 
    app.get('/', function (req, res) {
        if(req.protocol === 'https') {
            res.status(200).send('This is https visit!');
        } else {
            res.status(200).send('This is http visit!');
    }
});
httpsServer.listen(3001);

2.Node.js解决跨域问题

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

3.ajax访问

  • 一般情况下,使用微信小程序或Vue对后端进行访问时,都是需要https访问才可以,此时,我们只需要对Node程序进行https配置并且进行跨域处理即可。当然,配置方法上面已经给出。

示例代码:

var express = require("express");

var app = express();  //使用nodejs自带的http、https模块
var https = require('https');
var fs = require('fs');
var path = require("path");

//根据项目的路径导入生成的证书文件 
var privateKey  = fs.readFileSync(path.join(__dirname, '/public/215028913200048.key'), 'utf8');
var certificate = fs.readFileSync(path.join(__dirname, '/public/215028913200048.pem'), 'utf8');
var credentials = {key: privateKey, cert: certificate};

//创建http与HTTPS服务器
var httpsServer = https.createServer(credentials, app, function () {
    console.log('HTTPS Server is running on: https://localhost:%s');
});

//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

app.get('/',function (req, res) {
    res.status(200).send('This is https visit!');
    console.log("lalala");
});
app.get('/news', function (req, res) {
    console.log("历史性突破诞生啦");
})
httpsServer.listen(3000);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

页川叶川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值