Node.js所有请求的静态头(Node.js static header for all requests)
我想在node.js服务器中为所有传入请求设置静态头。 我目前的方法是为每个请求添加相同的4行标题(代码重复),我想解决这个问题,这里是我要添加的标题的一个示例:
app.post('/postJob', function(req,res){ console.log("request method :" + req.method); // Website you wish to allow to connect res.setHeader('Access-Control-Allow-Origin', '*'); // Request methods you wish to allow res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); // Request headers you wish to allow res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); // Set to true if you need the website to include cookies in the requests sent // to the API (e.g. in case you use sessions) res.setHeader('Access-Control-Allow-Credentials', true); res.setHeader('Content-Type', 'application/json');
有人知道定义它的方法吗?
I want to set a static header for all incoming request in my node.js server. My current way is to add for each request the same 4 lines of header(code duplication) and i want to fix that, here is one example of the header i want to add :
app.post('/postJob', function(req,res){ console.log("request method :" + req.method); // Website you wish to allow to connect res.setHeader('Access-Control-Allow-Origin', '*'); // Request methods you wish to allow res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); // Request headers you wish to allow res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); // Set to true if you need the website to include cookies in the requests sent // to the API (e.g. in case you use sessions) res.setHeader('Access-Control-Allow-Credentials', true); res.setHeader('Content-Type', 'application/json');
Someone knows a way to define that ?
原文:https://stackoverflow.com/questions/29058987
最满意答案
如果你的程序没有事件循环,你必须调用
dispatch_main()
:int main(int argc, char **argv) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ printf("Done outer async\n"); dispatch_async(dispatch_get_main_queue(),^{ printf("Done inner sync"); }); }); dispatch_main(); return 0; }
从文档:
该函数“停放”主线程并等待块被提交到主队列。 在主线程上调用
UIApplicationMain
(iOS),NSApplicationMain
(Mac OS X)或CFRunLoopRun
应用程序不得调用dispatch_main
。You have to call
dispatch_main()
if your program does not have an event loop:int main(int argc, char **argv) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ printf("Done outer async\n"); dispatch_async(dispatch_get_main_queue(),^{ printf("Done inner sync"); }); }); dispatch_main(); return 0; }
From the documentation:
This function "parks" the main thread and waits for blocks to be submitted to the main queue. Applications that call
UIApplicationMain
(iOS),NSApplicationMain
(Mac OS X), orCFRunLoopRun
on the main thread must not calldispatch_main
.
相关问答
更多-
结束了我的tearDown的以下操作。 while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) { NSLog(@"...Tearing Down Tests.."); [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinc ...
-
作为观察者,你无法控制从哪个线程/队列中调用observeValueForKeyPath。 你可以在这个方法所做的工作中将一个调度包装到你希望的队列中。 这将把观察的工作转移到高优先级队列。 只要确定你知道这项工作可能在触发观察的代码上运行在不同的线程/队列上。 如果您观察的对象不是线程安全的,则需要在将其余工作分派到其他队列之前进行检查。 As an observer you can't control what thread / queue observeValueForKeyPath is calle ...
-
解决方案是为您的解析函数添加一个闭包,即: func parse(_completion:(finished: Bool) -> Void) { // do your logic after finishing call your closure _completion(finished: true); } 如果你想调用你的解析函数,你必须做这样的事情: parse { (finished) -> Void in dispatch_async(dispatch_get_m ...
-
使用stringWithContentsOfURL:encoding:error:没有任何“错误” stringWithContentsOfURL:encoding:error:但是您在处理正在进行的请求时没有灵活性。 如果您需要执行以下任何操作,那么我会使用NSURLConnection : 在请求期间向用户显示完成百分比 向需要HTTP认证的主机发送请求 更复杂的数据处理(例如,下载比来自Web服务的典型JSON响应更多的数据),您可能希望以大块方式处理响应 取消请求(谢谢@马丁) There is n ...
-
为什么以下简单的中央调度程序无法正常工作?(Why will the following simple grand central dispatch program not work correctly?)[2023-08-29]
如果你的程序没有事件循环,你必须调用dispatch_main() : int main(int argc, char **argv) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ printf("Done outer async\n"); dispatch_async(dispatch_get_main_queue(),^{ ... -
它只是0而ul告诉编译器你希望它是一个无符号长整数以匹配函数签名。 dispatch_queue_t dispatch_get_global_queue(长优先级,无符号长标志); 如果你不添加ul,0将被视为整数,这可能会导致编译器警告。 请查看此处的文档。 https://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html It' ...
-
我的Grand Central Dispatch用法:我是否正确使用它?(My Grand Central Dispatch usage: Am I using it correctly?)[2021-11-10]
这基本上是正确的。 将数据检索分派到后台队列,然后将模型和UI更新分派回主队列。 做得好。 就其变慢而言,我没有看到任何可以解释的事情。 GCD引入了一些开销,但通常无法观察到。 这可能是一个“看着水壶永不沸腾”的问题。 但是,有几个无关的想法: 我可能会建议将排序移到for循环之外,但在reloadData之前。 你要分类150次。 如果进行插入排序,你可以在循环中完成,但我不认为这发生在这里。 我将排序移动到循环结束。 我不确定性能增益是否可以观察到,但应该有一些适度的改进。 您可能希望确保data不是 ... -
dispatch_sync() 技巧仅适用于串行队列,这是教程显示的内容。 dispatch_get_global_queue()返回并发队列,请参阅文档说明 : 提交给这些全局并发队列的块可以相互同时执行。 要处理全局并发队列,您应该使用提交块的组(也在该教程中提到),并使用dispatch_group_wait()等待整个组。 The dispatch_sync() trick will only work for serial queues, which is what that tutorial i ...
-
妥善处理Grand Central Dispatch I / O通道(Proper disposal of a Grand Central Dispatch I/O channel)[2022-09-20]
事实证明这是一个错误(雷达#10246694)。 进一步的实验似乎表明它只影响基于路径的调度通道,即使用dispatch_io_create_with_path()创建的通道,而不是dispatch_io_create() 。 It turns out this is a bug (radar #10246694). Further experimenting seems to indicate that it only affects path-based dispatch channels, i.e. ... -
也许doStuff1和doStuff2是死锁,还是其他东西阻塞了主线程? 4S具有多个内核,与4不同,所以可能会遇到一些您以前从未见过的多线程锁定问题。 你确定两个块实际上都在完成,并且主线程可用于运行结果块吗? 也许一些完整的代码(即doStuff1和2的主体)会有所帮助吗? Perhaps doStuff1 and doStuff2 are deadlocking, or something else is blocking the main thread? The 4S has multiple co ...