【发布时间】:2022-03-24 11:47:47
【问题描述】:
我尝试在 server.js 上实现速率限制器,但它在导致网站崩溃的下方给出了此错误。我是否错误地实施了速率限制器?我曾尝试更改 rate 和 apiLimiter 行位置,但没有奏效。 server.js 下面是错误日志。由于这是我第一次遇到此错误日志问题,我不确定在哪里寻找问题。这是我第一次在 server.js 中实现速率限制器,所以请指教。
Application error
An error occurred in the application and your page could not be served
server.js
import express from 'express'
import path from 'path'
import mongoose from 'mongoose'
import cors from 'cors'
import rateLimit from 'express-rate-limiter'
import config from './config'
import userRoute from './routes/userRoute'
import uploadRoute from './routes/uploadRoute'
import productRoute from './routes/productRoute'
import orderRoute from './routes/orderRoute'
const mongodbUrl = config.MONGODB_URL;
mongoose.connect(mongodbUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
}).catch((error) => console.log(error.reason))
const app = express()
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100
})
app.use(cors())
app.use(express.json())
app.use('/api/', apiLimiter)
app.use('/api/users', userRoute)
app.use('/api/uploads', uploadRoute)
app.use('/api/products', productRoute)
app.use('/api/orders', orderRoute)
app.get('/api/paypal/clientId', (req, res) => {
res.send({ clientId: config.PAYPAL_CLIENT_ID })
})
app.use('/uploads', express.static(path.join(__dirname, '/../uploads')))
app.use(express.static(path.join(__dirname, '/../frontend')))
app.get('*', (req, res) => {
res.sendFile(path.join(`${__dirname}/../frontend/index.html`))
})
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
const status = err.name && err.name === 'ValidationError' ? 400 : 500
res.status(status)
res.send({ message: err.message })
})
2021-05-29T05:11:02.707267+00:00 app[web.1]: this.__configuration = this.__buildConfiguration(options);
2021-05-29T05:11:02.707268+00:00 app[web.1]: ^
2021-05-29T05:11:02.707268+00:00 app[web.1]:
2021-05-29T05:11:02.707269+00:00 app[web.1]: TypeError: this.__buildConfiguration is not a function
2021-05-29T05:11:02.707269+00:00 app[web.1]: at module.exports (/app/node_modules/express-rate-limiter/index.js:13:33)
2021-05-29T05:11:02.707270+00:00 app[web.1]: at Object.<anonymous> (/app/dist/server.js:34:49)
2021-05-29T05:11:02.707270+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:774:30)
2021-05-29T05:11:02.707270+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
2021-05-29T05:11:02.707271+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:641:32)
2021-05-29T05:11:02.707271+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:556:12)
2021-05-29T05:11:02.707272+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
2021-05-29T05:11:02.707272+00:00 app[web.1]: at internal/main/run_main_module.js:17:11
2021-05-29T05:11:02.778624+00:00 heroku[web.1]: Process exited with status 1
2021-05-29T05:11:02.853093+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-29T05:11:59.000000+00:00 app[api]: Build started by user hillery262627@gmail.com
2021-05-29T05:13:01.344269+00:00 app[api]: Deploy c8670dff by user hillery262627@gmail.com
2021-05-29T05:13:01.344269+00:00 app[api]: Release v88 created by user hillery262627@gmail.com
2021-05-29T05:13:02.041520+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-29T05:13:03.000000+00:00 app[api]: Build succeeded
2021-05-29T05:13:06.356963+00:00 heroku[web.1]: Starting process with command `node dist/server.js`
2021-05-29T05:13:10.148201+00:00 app[web.1]: /app/node_modules/express-rate-limiter/index.js:13
2021-05-29T05:13:10.148263+00:00 app[web.1]: this.__configuration = this.__buildConfiguration(options);
2021-05-29T05:13:10.148272+00:00 app[web.1]: ^
2021-05-29T05:13:10.148276+00:00 app[web.1]:
2021-05-29T05:13:10.148276+00:00 app[web.1]: TypeError: this.__buildConfiguration is not a function
2021-05-29T05:13:10.148279+00:00 app[web.1]: at module.exports (/app/node_modules/express-rate-limiter/index.js:13:33)
2021-05-29T05:13:10.148279+00:00 app[web.1]: at Object.<anonymous> (/app/dist/server.js:34:49)
2021-05-29T05:13:10.148280+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:774:30)
2021-05-29T05:13:10.148280+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
2021-05-29T05:13:10.148280+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:641:32)
2021-05-29T05:13:10.148280+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:556:12)
2021-05-29T05:13:10.148280+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
2021-05-29T05:13:10.148281+00:00 app[web.1]: at internal/main/run_main_module.js:17:11
2021-05-29T05:13:10.200115+00:00 heroku[web.1]: Process exited with status 1
2021-05-29T05:13:10.283484+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-29T05:17:48.336323+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=revivaproject.herokuapp.com request_id=3f033042-c6d3-4c4b-bdbe-d0badd2d3d34 fwd="49.245.39.161" dyno= connect= service= status=503 bytes= protocol=https
2021-05-29T05:23:09.940782+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-29T05:23:13.553102+00:00 heroku[web.1]: Starting process with command `node dist/server.js`
2021-05-29T05:23:16.251662+00:00 app[web.1]: /app/node_modules/express-rate-limiter/index.js:13
2021-05-29T05:23:16.251678+00:00 app[web.1]: this.__configuration = this.__buildConfiguration(options);
2021-05-29T05:23:16.251678+00:00 app[web.1]: ^
2021-05-29T05:23:16.251679+00:00 app[web.1]:
2021-05-29T05:23:16.251679+00:00 app[web.1]: TypeError: this.__buildConfiguration is not a function
2021-05-29T05:23:16.251680+00:00 app[web.1]: at module.exports (/app/node_modules/express-rate-limiter/index.js:13:33)
2021-05-29T05:23:16.251680+00:00 app[web.1]: at Object.<anonymous> (/app/dist/server.js:34:49)
2021-05-29T05:23:16.251681+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:774:30)
2021-05-29T05:23:16.251681+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
2021-05-29T05:23:16.251682+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:641:32)
2021-05-29T05:23:16.251682+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:556:12)
2021-05-29T05:23:16.251683+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
2021-05-29T05:23:16.251683+00:00 app[web.1]: at internal/main/run_main_module.js:17:11
2021-05-29T05:23:16.318206+00:00 heroku[web.1]: Process exited with status 1
2021-05-29T05:23:16.378564+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-29T05:24:33.000000+00:00 app[api]: Build started by user hillery262627@gmail.com
2021-05-29T05:25:34.946347+00:00 app[api]: Release v89 created by user hillery262627@gmail.com
2021-05-29T05:25:34.946347+00:00 app[api]: Deploy c8670dff by user hillery262627@gmail.com
2021-05-29T05:25:35.219700+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-29T05:25:37.000000+00:00 app[api]: Build succeeded
2021-05-29T05:25:38.314459+00:00 heroku[web.1]: Starting process with command `node dist/server.js`
2021-05-29T05:25:40.788755+00:00 app[web.1]: /app/node_modules/express-rate-limiter/index.js:13
2021-05-29T05:25:40.788776+00:00 app[web.1]: this.__configuration = this.__buildConfiguration(options);
2021-05-29T05:25:40.788777+00:00 app[web.1]: ^
2021-05-29T05:25:40.788778+00:00 app[web.1]:
2021-05-29T05:25:40.788778+00:00 app[web.1]: TypeError: this.__buildConfiguration is not a function
2021-05-29T05:25:40.788779+00:00 app[web.1]: at module.exports (/app/node_modules/express-rate-limiter/index.js:13:33)
2021-05-29T05:25:40.788779+00:00 app[web.1]: at Object.<anonymous> (/app/dist/server.js:34:49)
2021-05-29T05:25:40.788779+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:774:30)
2021-05-29T05:25:40.788780+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
2021-05-29T05:25:40.788780+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:641:32)
2021-05-29T05:25:40.788780+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:556:12)
2021-05-29T05:25:40.788781+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
2021-05-29T05:25:40.788781+00:00 app[web.1]: at internal/main/run_main_module.js:17:11
2021-05-29T05:25:40.864253+00:00 heroku[web.1]: Process exited with status 1
2021-05-29T05:25:40.952164+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-29T05:34:33.335208+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=revivaproject.herokuapp.com request_id=3a904ea7-930c-42e6-a9af-16e333a4064a fwd="49.245.39.161" dyno= connect= service= status=503 bytes= protocol=https
2021-05-29T05:37:25.482013+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=revivaproject.herokuapp.com request_id=ebd52cb6-bb09-481b-bdb7-91d8c8993766 fwd="49.245.39.161" dyno= connect= service= status=503 bytes= protocol=https
2021-05-29T05:37:26.504268+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=revivaproject.herokuapp.com request_id=f7f3e1ec-ea62-4ea9-8712-b1178c56abca fwd="49.245.39.161" dyno=
connect= service= status=503 bytes= protocol=https
2021-05-29T05:38:36.526750+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=revivaproject.herokuapp.com request_id=0402ac65-49cb-4b09-a44e-6aaf428c23dd fwd="49.245.39.161" dyno= connect= service= status=503 bytes= protocol=https
2021-05-29T05:38:37.438382+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=revivaproject.herokuapp.com request_id=4389454b-a233-45d8-bf60-a3f0e7349504 fwd="49.245.39.161" dyno= connect= service= status=503 bytes= protocol=https
2021-05-29T06:09:11.655622+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-29T06:09:15.444774+00:00 heroku[web.1]: Starting process with command `node dist/server.js`
2021-05-29T06:09:17.936977+00:00 app[web.1]: /app/node_modules/express-rate-limiter/index.js:13
2021-05-29T06:09:17.937002+00:00 app[web.1]: this.__configuration = this.__buildConfiguration(options);
2021-05-29T06:09:17.937003+00:00 app[web.1]: ^
2021-05-29T06:09:17.937003+00:00 app[web.1]:
2021-05-29T06:09:17.937003+00:00 app[web.1]: TypeError: this.__buildConfiguration is not a function
2021-05-29T06:09:17.937004+00:00 app[web.1]: at module.exports (/app/node_modules/express-rate-limiter/index.js:13:33)
2021-05-29T06:09:17.937004+00:00 app[web.1]: at Object.<anonymous> (/app/dist/server.js:34:49)
2021-05-29T06:09:17.937005+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:774:30)
2021-05-29T06:09:17.937005+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
2021-05-29T06:09:17.937005+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:641:32)
2021-05-29T06:09:17.937006+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:556:12)
2021-05-29T06:09:17.937006+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
2021-05-29T06:09:17.937007+00:00 app[web.1]: at internal/main/run_main_module.js:17:11
2021-05-29T06:09:17.980716+00:00 heroku[web.1]: Process exited with status 1
2021-05-29T06:09:18.047009+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-29T06:48:19.910733+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=revivaproject.herokuapp.com request_id=b65d1d7c-50b2-4b63-b305-400459ed69ba fwd="49.245.39.161" dyno= connect= service= status=503 bytes= protocol=https
2021-05-29T06:48:21.301919+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=revivaproject.herokuapp.com request_id=61fe1968-f463-4c52-b3a8-8d517d0a157d fwd="49.245.39.161" dyno= connect= service= status=503 bytes= protocol=https
2021-05-29T06:48:33.577825+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=revivaproject.herokuapp.com request_id=9831ac5d-869a-4f05-a2f3-6a0422efba77 fwd="49.245.39.161" dyno= connect= service= status=503 bytes= protocol=https
2021-05-29T06:48:34.410206+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=revivaproject.herokuapp.com request_id=cd55e067-ced5-4689-a815-5725e7daa151 fwd="49.245.39.161" dyno= connect= service= status=503 bytes= protocol=https
2021-05-29T06:55:56.000000+00:00 app[api]: Build started by user hillery262627@gmail.com
2021-05-29T06:56:52.630557+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-29T06:56:52.473357+00:00 app[api]: Deploy e28a52be by user hillery262627@gmail.com
2021-05-29T06:56:52.473357+00:00 app[api]: Release v90 created by user hillery262627@gmail.com
2021-05-29T06:56:54.000000+00:00 app[api]: Build succeeded
2021-05-29T06:56:55.920729+00:00 heroku[web.1]: Starting process with command `node dist/server.js`
2021-05-29T06:56:58.334876+00:00 heroku[web.1]: Process exited with status 1
2021-05-29T06:56:58.401966+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-29T06:56:58.405968+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-29T06:56:58.278359+00:00 app[web.1]: /app/node_modules/express-rate-limiter/index.js:13
2021-05-29T06:56:58.278373+00:00 app[web.1]: this.__configuration = this.__buildConfiguration(options);
2021-05-29T06:56:58.278374+00:00 app[web.1]: ^
2021-05-29T06:56:58.278374+00:00 app[web.1]:
2021-05-29T06:56:58.278374+00:00 app[web.1]: TypeError: this.__buildConfiguration is not a function
2021-05-29T06:56:58.278375+00:00 app[web.1]: at module.exports (/app/node_modules/express-rate-limiter/index.js:13:33)
2021-05-29T06:56:58.278375+00:00 app[web.1]: at Object.<anonymous> (/app/dist/server.js:34:52)
2021-05-29T06:56:58.278375+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:774:30)
2021-05-29T06:56:58.278376+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
2021-05-29T06:56:58.278376+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:641:32)
2021-05-29T06:56:58.278376+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:556:12)
2021-05-29T06:56:58.278377+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
2021-05-29T06:56:58.278377+00:00 app[web.1]: at internal/main/run_main_module.js:17:11
2021-05-29T06:57:01.716901+00:00 heroku[web.1]: Starting process with command `node dist/server.js`
2021-05-29T06:57:04.120156+00:00 heroku[web.1]: Process exited with status 1
2021-05-29T06:57:04.188477+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-29T06:57:04.078916+00:00 app[web.1]: /app/node_modules/express-rate-limiter/index.js:13
2021-05-29T06:57:04.078967+00:00 app[web.1]: this.__configuration = this.__buildConfiguration(options);
2021-05-29T06:57:04.078967+00:00 app[web.1]: ^
2021-05-29T06:57:04.078968+00:00 app[web.1]:
2021-05-29T06:57:04.078968+00:00 app[web.1]: TypeError: this.__buildConfiguration is not a function
2021-05-29T06:57:04.078982+00:00 app[web.1]: at module.exports (/app/node_modules/express-rate-limiter/index.js:13:33)
2021-05-29T06:57:04.078983+00:00 app[web.1]: at Object.<anonymous> (/app/dist/server.js:34:52)
2021-05-29T06:57:04.078983+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:774:30)
2021-05-29T06:57:04.078983+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
2021-05-29T06:57:04.078983+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:641:32)
2021-05-29T06:57:04.078984+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:556:12)
2021-05-29T06:57:04.078984+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
2021-05-29T06:57:04.078984+00:00 app[web.1]: at internal/main/run_main_module.js:17:11
2021-05-29T06:57:05.387430+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=revivaproject.herokuapp.com request_id=54289191-9ec6-4ca9-ad4c-49395f5b3bf4 fwd="49.245.39.161" dyno= connect= service= status=503 bytes= protocol=https
【问题讨论】:
-
你在使用npmjs.com/package/express-rate-limit吗?如果是这样,您应该使用:
import rateLimit from 'express-rate-limit'。另一个库是npmjs.com/package/express-rate-limiter,但它使用了其他配置方法。 -
哦,非常感谢!!!我没有意识到有两种不同类型的速率限制 npm 库