Node.js:一个路由2次 request

本文介绍如何使用Node.js快速创建一个简易服务器,并解释了默认情况下每个路由请求会被计数两次的原因,同时提供了解决方案。

1313

 

node.js 和其他服务器端编程语言有很大区别,语言上的特性优点已经在前面分享过,这里就不说了.

与其他语言相比,还有一个区别,node.js 自身实现了一个服务器,而其他服务器端语言大部分要依赖Apach ,nginx ,IIS 等.

 

node.js 可以用几句代码轻松实现:

var http = require('http');        


var webServer = function (req, res){ 
		res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'}); 
		res.write('<html><body>'); 
		res.write('<h2>Hello World!</h2>'); 
		res.end('</body></html>'); 
};

var createWebServer=http.createServer(webServer);

createWebServer.listen(8000);
console.log('listen 8000');

 

上面我们就实现了,不论什么路由,服务器端都会返回 Hello World!

但是:如果你看控制台log 会发现每个url的请求,都有2次 request .

不信可以把 console.log(req);

 

放到 webServer 函数中间,你一定会发现每个请求都输出2次 request 信息.

原因是因为node.js 每个请求默认还有一个 对 favicon.ico 资源的请求.Express 貌似对此请求做了特殊处理.

我们可以这样避免 favicon.ico 资源的请求

var http = require('http');        

var webServer = function (req, res){ 
	if(req.url!='/favicon.ico'){
		res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'}); 
		res.write('<html><body>'); 
		res.write('<h2>Hello World!</h2>'); 
		res.end('</body></html>'); 
	}
};

var createWebServer=http.createServer(webServer);

createWebServer.listen(8000);
console.log('listen 8000');

 

 

原文:http://yijiebuyi.com/blog/ee3c1f200f2824e581900509d9c9d246.html

本文转自:Node.js:一个路由2次 request

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

HotelDetailView.vue:114 GET http://localhost:8080/api/hotels/1/room-types 403 (Forbidden) dispatchXhrRequest @ xhr.js:195 xhr @ xhr.js:15 dispatchRequest @ dispatchRequest.js:51 Promise.then _request @ Axios.js:163 request @ Axios.js:40 Axios.<computed> @ Axios.js:213 wrap @ bind.js:5 fetchHotelDetails @ HotelDetailView.vue:114 (匿名) @ runtime-core.esm-bundler.js:2836 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 hook.__weh.hook.__weh @ runtime-core.esm-bundler.js:2816 flushPostFlushCbs @ runtime-core.esm-bundler.js:385 flushJobs @ runtime-core.esm-bundler.js:427 Promise.then queueFlush @ runtime-core.esm-bundler.js:322 queueJob @ runtime-core.esm-bundler.js:317 effect2.scheduler @ runtime-core.esm-bundler.js:5480 trigger @ reactivity.esm-bundler.js:265 endBatch @ reactivity.esm-bundler.js:323 notify @ reactivity.esm-bundler.js:614 trigger @ reactivity.esm-bundler.js:588 set value @ reactivity.esm-bundler.js:1465 finalizeNavigation @ vue-router.mjs:3503 (匿名) @ vue-router.mjs:3368 Promise.then pushWithRedirect @ vue-router.mjs:3335 push @ vue-router.mjs:3260 navigate @ vue-router.mjs:2315 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 invoker @ runtime-dom.esm-bundler.js:729 api.js:46 Forbidden access - you do not have permission to access this resource. (匿名) @ api.js:46 Promise.then _request @ Axios.js:163 request @ Axios.js:40 Axios.<computed> @ Axios.js:213 wrap @ bind.js:5 fetchHotelDetails @ HotelDetailView.vue:114 (匿名) @ runtime-core.esm-bundler.js:2836 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 hook.__weh.hook.__weh @ runtime-core.esm-bundler.js:2816 flushPostFlushCbs @ runtime-core.esm-bundler.js:385 flushJobs @ runtime-core.esm-bundler.js:427 Promise.then queueFlush @ runtime-core.esm-bundler.js:322 queueJob @ runtime-core.esm-bundler.js:317 effect2.scheduler @ runtime-core.esm-bundler.js:5480 trigger @ reactivity.esm-bundler.js:265 endBatch @ reactivity.esm-bundler.js:323 notify @ reactivity.esm-bundler.js:614 trigger @ reactivity.esm-bundler.js:588 set value @ reactivity.esm-bundler.js:1465 finalizeNavigation @ vue-router.mjs:3503 (匿名) @ vue-router.mjs:3368 Promise.then pushWithRedirect @ vue-router.mjs:3335 push @ vue-router.mjs:3260 navigate @ vue-router.mjs:2315 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 invoker @ runtime-dom.esm-bundler.js:729 HotelDetailView.vue:122 Failed to fetch hotel details or room types: AxiosError {message: 'Request failed with status code 403', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …} fetchHotelDetails @ HotelDetailView.vue:122 await in fetchHotelDetails (匿名) @ runtime-core.esm-bundler.js:2836 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 hook.__weh.hook.__weh @ runtime-core.esm-bundler.js:2816 flushPostFlushCbs @ runtime-core.esm-bundler.js:385 flushJobs @ runtime-core.esm-bundler.js:427 Promise.then queueFlush @ runtime-core.esm-bundler.js:322 queueJob @ runtime-core.esm-bundler.js:317 effect2.scheduler @ runtime-core.esm-bundler.js:5480 trigger @ reactivity.esm-bundler.js:265 endBatch @ reactivity.esm-bundler.js:323 notify @ reactivity.esm-bundler.js:614 trigger @ reactivity.esm-bundler.js:588 set value @ reactivity.esm-bundler.js:1465 finalizeNavigation @ vue-router.mjs:3503 (匿名) @ vue-router.mjs:3368 Promise.then pushWithRedirect @ vue-router.mjs:3335 push @ vue-router.mjs:3260 navigate @ vue-router.mjs:2315 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 invoker @ runtime-dom.esm-bundler.js:729 authStore.js:49 POST http://localhost:8080/api/users/login 403 (Forbidden) dispatchXhrRequest @ xhr.js:195 xhr @ xhr.js:15 dispatchRequest @ dispatchRequest.js:51 Promise.then _request @ Axios.js:163 request @ Axios.js:40 httpMethod @ Axios.js:226 wrap @ bind.js:5 login @ authStore.js:49 wrappedAction @ pinia.mjs:1399 store.<computed> @ pinia.mjs:932 handleLogin @ Auth.vue:219 await in handleLogin cache.<computed>.cache.<computed> @ runtime-dom.esm-bundler.js:1713 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 invoker @ runtime-dom.esm-bundler.js:729 api.js:46 Forbidden access - you do not have permission to access this resource. (匿名) @ api.js:46 Promise.then _request @ Axios.js:163 request @ Axios.js:40 httpMethod @ Axios.js:226 wrap @ bind.js:5 login @ authStore.js:49 wrappedAction @ pinia.mjs:1399 store.<computed> @ pinia.mjs:932 handleLogin @ Auth.vue:219 await in handleLogin cache.<computed>.cache.<computed> @ runtime-dom.esm-bundler.js:1713 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 invoker @ runtime-dom.esm-bundler.js:729 settle.js:19 Uncaught (in promise) AxiosError {message: 'Request failed with status code 403', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}
06-24
tools.ts:40 Download the React DevTools for a better development experience: https://react.dev/link/react-devtools app.tsx:35 {success: true, data: {…}, errorMessage: '', errorCode: ''} request.ts:220 PUT http://localhost:3000/api/v1/admin/reservations/bf677b0d-a461-408d-a3ed-057f7303ad81 500 (Internal Server Error) dispatchXhrRequest @ xhr.js:220 xhrAdapter @ xhr.js:16 dispatchRequest @ dispatchRequest.js:58 Promise.then request @ Axios.js:89 wrap @ bind.js:9 (匿名) @ request.ts:220 request @ request.ts:218 editModelByIdAPI @ reservations.ts:64 onClick @ index.tsx:140 (匿名) @ button.js:188 executeDispatch @ react-dom-client.development.js:19116 runWithFiberInDEV @ react-dom-client.development.js:871 processDispatchQueue @ react-dom-client.development.js:19166 (匿名) @ react-dom-client.development.js:19767 batchedUpdates$1 @ react-dom-client.development.js:3255 dispatchEventForPluginEventSystem @ react-dom-client.development.js:19320 dispatchEvent @ react-dom-client.development.js:23585 dispatchDiscreteEvent @ react-dom-client.development.js:23553 <button> exports.createElement @ react.development.js:1054 Button @ button.js:261 react_stack_bottom_frame @ react-dom-client.development.js:25904 renderWithHooks @ react-dom-client.development.js:7662 updateForwardRef @ react-dom-client.development.js:9724 beginWork @ react-dom-client.development.js:12117 runWithFiberInDEV @ react-dom-client.development.js:871 performUnitOfWork @ react-dom-client.development.js:17641 workLoopSync @ react-dom-client.development.js:17469 renderRootSync @ react-dom-client.development.js:17450 performWorkOnRoot @ react-dom-client.development.js:16504 performWorkOnRootViaSchedulerTask @ react-dom-client.development.js:18957 performWorkUntilDeadline @ scheduler.development.js:45 queue.<computed> @ task.js:62 run @ task.js:36 eventListener @ task.js:47 <Button> exports.jsxDEV @ react-jsx-dev-runtime.development.js:335 render @ index.tsx:136 columnRender @ columnRender.js:127 render @ genProColumnTo
最新发布
10-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值