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);

你可能感兴趣的:(Node.js基础知识)