Express服务器需要500ms才能返回304响应

时间:2017-03-31 19:27:23

标签: javascript node.js express

我有一个非常简单的快递服务器。它有static-serve并使用cacheControl来提供文件。我将const path = require('path'); const express = require('express'); const compression = require('compression'); const serveStatic = require('serve-static'); const PORT = process.env.PORT || 9095; const app = express(); app.use(compression({ threshold: 0 })); app.use(/regex pattern here/, serveStatic(path.join(__dirname, '../dist/client/assets'), { cacheControl: false })); app.listen(PORT, (error) => { console.log(error || `Listening on port ${PORT}`); }); 设置为false,因此它仅使用ETag。

服务器将为文件返回304(这是所需的),但这需要大约500ms。这与实际发送文件所用的时间相同。

我可以做一些配置(除了Varnish和nginx之类的东西)来加快速度吗?

服务器只有大约6个不同的文件可供使用,都是静态的。

Express服务器代码:

 SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cxf' defined in class path resource [META-INF/cxf/cxf.xml]: Initialization of bean failed; nested exception is org.apache.cxf.bus.extension.ExtensionException: Could not find constructor for class org.apache.cxf.ws.policy.PolicyDataEngineImpl for args [org.apache.cxf.bus.spring.SpringBus@421fc207].
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5524)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: org.apache.cxf.bus.extension.ExtensionException: Could not find constructor for class org.apache.cxf.ws.policy.PolicyDataEngineImpl for args [org.apache.cxf.bus.spring.SpringBus@421fc207].
        at org.apache.cxf.bus.extension.Extension.load(Extension.java:275)
        at org.apache.cxf.bus.extension.ExtensionManagerImpl.loadAndRegister(ExtensionManagerImpl.java:213)
        at org.apache.cxf.bus.extension.ExtensionManagerImpl.initialize(ExtensionManagerImpl.java:121)
        at org.apache.cxf.bus.extension.ExtensionManagerBus.doInitializeInternal(ExtensionManagerBus.java:295)
        at org.apache.cxf.bus.extension.ExtensionManagerBus.initialize(ExtensionManagerBus.java:285)
        at org.apache.cxf.bus.spring.SpringBus.setApplicationContext(SpringBus.java:88)
        at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:119)
        at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:94)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
        ... 20 more
    Caused by: java.lang.NoSuchMethodException: org.apache.cxf.ws.policy.PolicyDataEngineImpl.<init>()
        at java.lang.Class.getConstructor0(Class.java:2892)
        at java.lang.Class.getConstructor(Class.java:1723)
        at org.apache.cxf.bus.extension.Extension.load(Extension.java:249)
        ... 30 more

2 个答案:

答案 0 :(得分:1)

您应该使用nginx或类似的东西来提供静态内容,因为node serve-static模块需要从文件内容计算哈希值。所以这甚至比仅仅在本地机器上提供文件更慢。 这里深入研究了服务静态机制https://evanhahn.com/express-dot-static-deep-dive/

答案 1 :(得分:0)

事实证明,诀窍是使用NODE_ENV=production运行快速服务器。

显然,这告诉Express做了一些优化,其中一个似乎完全符合我的要求。从大约500ms到大约40ms返回304,这要好得多。

参考:https://expressjs.com/en/advanced/best-practice-performance.html#in-environment