The content of this article is about the method of building a web server with nodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The front-end often encounters cross-domain problems when obtaining data. This problem can be solved by using nginx as a reverse proxy. However, nginx is a middleware proxy, and the web server addresses deployed by different developers may be different, so the configuration of nginx cannot be universal.
If you can have a client proxy and submit it with the project source code, you can eliminate the need for proxy configurations for different developers. webpack-dev-server is such a client proxy, but if the project does not use webpack, there is no way to use it. Can you imitate and write a simple web server for non-webpack projects? Below is the code, I hope you guys can criticize and correct me.
const request = require('request'); const express = require('express'); const path = require('path'); const app = express(); // 代理配置 const proxyTable = { '/api': { target: 'http://localhost/api' } }; app.use(function(req, res,next) { const url = req.url; if (req.method == 'OPTIONS') { console.log('options_url: ', url); // 设置cors 跨域 // res.header("Access-Control-Allow-Origin", req.headers.origin || '*'); // res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With"); // res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); // 设置 cookie // res.header("Access-Control-Allow-Credentials", true); res.status(200).send('OK'); return; } // console.log('req_url: ', url); next(); }); // 设置静态目录 app.use(express.static(path.join(__dirname, 'static'))); app.use('/', function(req, res) { const url = req.url; const proxy = Object.keys(proxyTable); let not_found = true; for (let index = 0; index < proxy.length; index++) { const k = proxy[index]; const i = url.indexOf(k); if (i >= 0) { not_found = false; const element = proxyTable[k]; const newUrl = element.target + url.slice(i+k.length); req.pipe(request({url: newUrl, timeout: 60000},(err)=>{ if(err){ console.log('error_url: ', err.code,url); res.status(500).send(''); } })).pipe(res); break; } } if(not_found) { console.log('not_found_url: ', url); res.status(404).send('Not found'); } else { console.log('proxy_url: ', url); } }); // 监听端口 const PORT = 8080; app.listen(PORT, () => { console.log('HTTP Server is running on: http://localhost:%s', PORT); });
PS: static puts static pages
The above is the detailed content of How to build a web server with nodejs. For more information, please follow other related articles on the PHP Chinese website!