1. nodejs
如何启动两个端口, 比如一个一个proxy一个service
const proxy = require('./src/proxy'); //一些变量可以加载起来 const service = require('./src/service'); proxy.start(); //在最外围的不用await. service.start(); module.exports.proxy = proxy; module.exports.service = service;
z在start里面做什么? 比如开一个httpserver
const _httpServer = http.createServer(app); const corsOptions = { origin: 'http://localhost:3000', methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', preflightContinue: false, credentials: true, }; const start = async () => { // confiugraiton of passport is async // Before we can bind the routes - we need the passport const passport = await require('./passport').configure(); //这里初始化好了passport const routes = require('./routes'); app.use(cors(corsOptions)); app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: false, })); app.use(passport.initialize()); app.use(passport.session()); app.use(express.json()); app.use(express.urlencoded({extended: true})); app.use('/', routes); await _httpServer.listen(port); console.log(`Service Listening on ${port}`); app.emit('ready'); return app; }; module.exports.start = start;
2. passport是什么
截获请求, 如果用户不存在, 则停止
会调用里面的use方法, cb成功, 则向后传递
https://github.com/jaredhanson/passport/tree/master/lib
3. nodejs中export vs module.exports的区别
https://blog.csdn.net/yanyongtao1/article/details/78970992
load()函数最终返回module.exports;
var load = function(exports,module){
return module.exports;
}
var exported = load(module.exports,module);
也就是说,默认情况下node准备的exports和module.exports实际上指向同一个空对象
不可以exports= {hello:hello};//对象会被制空
但是可以 export.hello = hello
4. 转发请求 express-http-proxy
https://www.jianshu.com/p/846e8b555ead
proxy(host, options); options: { forwardPath:fuctoin(){ return '' } 将请求转发。return string forwardPathAsync: return new Promise((resovle, reject)=>{ resolve(''); }), filter: (req, res)=>{ //那些请求可以转发 return req.method == 'GET'; }, intercept:function(rsp, data, req, res, callback) { // rsp - original response from the target data = JSON.parse(data.toString('utf8')); callback(null, JSON.stringify(data)); } //将响应返回给客户端之前 decorateRequest: function(proxyReq, originalReq) { // you can update headers proxyReq.headers['Content-Type'] = 'text/html'; // you can change the method proxyReq.method = 'GET'; // you can munge the bodyContent. proxyReq.bodyContent = proxyReq.bodyContent.replace(/losing/, 'winning!'); return proxyReq; } 在请求通过代理转发至目标主机之 timeout https }