【问题标题】:Two questions, 1. On express router 2.request, response两个问题,1.关于express router 2.request,response
【发布时间】:2018-12-29 07:50:32
【问题描述】:

[环境] 节点.js, 表示, 反应, nginx

[第一个问题] 如果我重新加载了除根 url 之外的网站,我收到一个错误“无法获取 /...”

[第二个问题] 当我通过 ajax 向服务器端请求超过 6 次时,网站停止并且没有响应。如果我重新启动服务器,我可以再次从服务器获得响应 5 次... 这个错误对我来说非常重要。

我的代码..! [客户]

	
  // request to server
  fetchInfo = async () => {
		const { id } = this.props.params;
		const room = await webster.get("/api/v1/conference/"+id);

		this.setState({
			info: room
		});
	}
  
  //webster.get - ajax
  function get(url, data) {
	const get =	$.ajax({
		url,
		data,
		type: "get",
		dataType: "json",
	});
	const error = get.fail(() => {
		throw new Error("Error occured in request");
	});
	const info = get.done(info => {
		return info;
	});
	return info || error;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

[后端]

//app.js

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Attach database pool for req object
app.use((req, res, next) => {
	req.db = pool;
	allowCrossDomain;
	next();
});

var allowCrossDomain = function(req, res, next) {
	res.header('Access-Control-Allow-Origin', '*');
	res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
	res.header('Access-Control-Allow-Headers', 'Content-Type'); next();
} ;

//set route for prod
app.get('/', (req, res) => {
	res.sendFile(path.join(__dirname + '/../public/dist'+ 'index.html'));
});

app.use('/', express.static(path.join(__dirname + '/../public')));
app.use('/api/v1/user', user);
app.use('/api/v1/conference', conference);

module.exports = {
	app
};



//api/conference/index.js
const router = require('express').Router();
const _get = require('./get');

router.get('/', (req, res) => {
	_get.searchConferenceRoom(req, res);
});

router.get('/:id', (req, res) => {
	_get.getRoomInfo(req, res); //request query to db
});

module.exports = router;



//www.js - exec file
const { app } = require('../app');
const port = require('../settings.json').http.port;
const httpServer = require('http').Server(app);

app.set('port', process.env.PORT || port);

//set port
httpServer.listen(app.get('port'), () => {
	console.log('Express started on http://localhost:' + app.get('port'));
});

【问题讨论】:

    标签: node.js reactjs express nginx


    【解决方案1】:

    关于你的第一个问题

    如果我重新加载了除根 url 之外的网站,我得到一个错误“无法获取 /...”

    这是意料之中的,因为当您浏览路由器链接时,Javascript 会运行并操作地址栏中的 URL,而不会导致页面刷新,这反过来会进行页面转换

    解决方案是做一个“包罗万象”的路线

    只需在服务器上设置一个包罗万象,将所有请求发送到索引 index.html

    类似的东西

    app.get('/*', function (req, res) {
        res.sendFile(path.join(__dirname, '/../../client/build', 'index.html'));
    });
    

    但请确保在您的休息 api 之后执行此路由

    关于第二个问题,它很不清楚,如果我想猜测一下,我认为请求的处理程序会卡住并且在您结束它之前不会响应客户端

    【讨论】:

    • 我终于找到了我的错误代码!这些在方法“getRoomInfo”中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 2010-10-07
    相关资源
    最近更新 更多