I’m trying to analyze and rebuild some code written by someone that currently consists of an older version of express.js (4.18.2) to a current version of express.js.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const onem2mParser = bodyParser.text(
{
limit: '5mb',
type: 'application/onem2m-resource+xml;application/xml;application/json;application/vnd.onem2m-res+xml;application/vnd.onem2m-res+json'
}
);
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, X-M2M-RI, X-M2M-RVI, X-M2M-RSC, Accept, X-M2M-Origin, Locale');
res.header('Access-Control-Expose-Headers', 'Origin, X-Requested-With, Content-Type, X-M2M-RI, X-M2M-RVI, X-M2M-RSC, Accept, X-M2M-Origin, Locale');
(req.method == 'OPTIONS') ? res.sendStatus(200) : next();
});
# Where The error occurs
app.post(onem2mParser, function (request, response) {
...
}
I have trouble with app.post() above, and the current version of express.js is throwing the following error:
ERROR Uncaught Exception
{
"errorType": "TypeError",
"errorMessage": "path must be a string, array of strings, or regular expression",
"stack": [
"TypeError: path must be a string, array of strings, or regular expression",
" at pathToRegexp (/var/task/node_modules/path-to-regexp/index.js:69:11)",
" at new Layer (/var/task/node_modules/express/lib/router/layer.js:45:17)",
" at Function.route (/var/task/node_modules/express/lib/router/index.js:505:15)",
" at app.<computed> [as get] (/var/task/node_modules/express/lib/application.js:498:30)",
" at Object.<anonymous> (/var/task/src/get.js:80:5)",
" at Module._compile (node:internal/modules/cjs/loader:1554:14)",
" at Object..js (node:internal/modules/cjs/loader:1706:10)",
" at Module.load (node:internal/modules/cjs/loader:1289:32)",
" at Function._load (node:internal/modules/cjs/loader:1108:12)",
" at TracingChannel.traceSync (node:diagnostics_channel:322:14)"
]
}
In this part, I used the parser as a middleware with the help of chatgpt, but an error is occuring in another module that is using the code in this part.
const router = express.Router();
router.use(onem2mParser);
router.post('/', function(req, res) => ...);
app.use('/', router);
ERROR TypeError: ee.on is not a function
at first (/var/task/node_modules/ee-first/index.js:43:10)
at onSocket (/var/task/node_modules/on-finished/index.js:115:16)
at attachFinishedListener (/var/task/node_modules/on-finished/index.js:120:5)
at attachListener (/var/task/node_modules/on-finished/index.js:147:5)
at onFinished (/var/task/node_modules/on-finished/index.js:53:3)
at send (/var/task/node_modules/finalhandler/index.js:314:3)
at /var/task/node_modules/finalhandler/index.js:133:5
at /var/task/node_modules/express/lib/router/index.js:646:15
at next (/var/task/node_modules/express/lib/router/index.js:265:14)
at textParser (/var/task/node_modules/express/node_modules/body-parser/lib/types/text.js:78:7)
Finally, I have two questions.
- What does it mean to insert
bodyParser instead of string type into path parameter in app.post()? Is the path then ‘/’?
- How do I fix the code to work well with the same logic in the latest 4.x version of express.js?