---
title: "[Node.js] 学习笔记 - express router"
author: "cute.spring"
date: "2014-04-20T18:46:48Z"
tags: []
categories: ["开源软件"]
slug: "node-js-express-router"
draft: false
lastmod: "2014-04-20T18:46:48Z"
id: 2050149
url: "https://cute-spring.iteye.com/blog/2050149"
---

参考:
http://stackoverflow.com/questions/12695591/node-js-express-js-how-does-app-router-work

The best option is to put all of your static resources under a specific folder. (IE /static) You can then mount static to that path so that it only runs when the path starts with /static:


```javaScript
app.use('/static', express.static(__dirname + '/static'));
```


In this situation, you'd put this above router. This avoids processing other middleware/the router if a file is present, but to be honest, I doubt you'll gain that much.

You could also use staticCache, which caches static resources in-memory so that you don't have to hit the disk for commonly requested files. (Warning: staticCache will apparently be removed in the future.)

However, I don't think staticCache caches negative answers (when a file does not exist), so it doesn't help if you've put staticCache above router without mounting it to a path.