In my nodeJS application I used following line of code to get req.body
app.use(bodyParser.urlencoded({
extended: false
}));
This is working fine, but one request to my application throws following error:
UnsupportedMediaTypeError: unsupported charset "ISO-8859-1"
at urlencodedParser (/var/www/payment/node_modules/body-parser/lib/types/urlencoded.js:108:12)
at Layer.handle [as handle_request] (/var/www/payment/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/var/www/payment/node_modules/express/lib/router/index.js:317:13)
at /var/www/payment/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/var/www/payment/node_modules/express/lib/router/index.js:335:12)
at next (/var/www/payment/node_modules/express/lib/router/index.js:275:10)
at jsonParser (/var/www/payment/node_modules/body-parser/lib/types/json.js:118:7)
at Layer.handle [as handle_request] (/var/www/payment/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/var/www/payment/node_modules/express/lib/router/index.js:317:13)
at /var/www/payment/node_modules/express/lib/router/index.js:284:7
If I remove the previous line, then it works for that request, but in all other requests, I am unable to retrieve data. Is there any way to fix this?
You can modify the original request header, to UTF-8.
I found no easy way to intercept the urlencoded parser, so i just changed the encoding to utf8
Related
So I'm following this guide MEAN STACK GUIDE
I have encountered an error while on number 2
I can't fix it myself since I'm new in studying mean stack
any help here?
here's the full error by the way
Error: No default engine was specified and no extension was provided.
at new View (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/view.js:61:11)
at Function.render (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/response.js:1008:7)
at /Users/angelocolmenares/Desktop/Carwash/mean-secure/app.js:32:7
at Layer.handle_error (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:315:13)
at /Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:335:12)
at next (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:275:10)
at /Users/angelocolmenares/Desktop/Carwash/mean-secure/app.js:21:3
GET /favicon.ico 404 1.584 ms - 1428
Error: No default engine was specified and no extension was provided.
at new View (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/view.js:61:11)
at Function.render (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/response.js:1008:7)
at /Users/angelocolmenares/Desktop/Carwash/mean-secure/app.js:32:7
at Layer.handle_error (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:315:13)
at /Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:335:12)
at next (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:275:10)
at /Users/angelocolmenares/Desktop/Carwash/mean-secure/app.js:21:3
GET / 404 0.999 ms - 1428
Error: No default engine was specified and no extension was provided.
at new View (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/view.js:61:11)
at Function.render (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/response.js:1008:7)
at /Users/angelocolmenares/Desktop/Carwash/mean-secure/app.js:32:7
at Layer.handle_error (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:315:13)
at /Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:335:12)
at next (/Users/angelocolmenares/Desktop/Carwash/mean-secure/node_modules/express/lib/router/index.js:275:10)
at /Users/angelocolmenares/Desktop/Carwash/mean-secure/app.js:21:3
You have not defined any routes in routes/api.js. So for now you have localhost:3000/api and this one is the only route which is accessible.
Plus you are getting No default engine was specified and no extension was provided. error because of line no 31 in app.js because you want to render error page when you are hitting localhost:3000/api/profile. You don't have any route which matches this route. You can solve it by changing those line to:
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500)
res.send({ 'error': 'no route found!' });
});
for more details about render from express read : documentation
When test my server in Postman, the status is 404 NOT_FOUND:
{"error":{"statusCode":404,"name":"Error","message":"Shared class
\"App\" has no method handling GET
/56fd2bf04fbf42f357f6b112/versions","stack":"Error: Shared class
\"App\" has no method handling GET
/56fd2bf04fbf42f357f6b112/versions\n at restRemoteMethodNotFound
(D:\PKL\cyclone-backend\node_modules\strong-remoting\lib\rest-adapter.js:388:17)\n
at Layer.handle [as handle_request]
(D:\PKL\cyclone-backend\node_modules\express\lib\router\layer.js:95:5)\n
at trim_prefix
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:317:13)\n
at
D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:284:7\n
at Function.process_params
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:335:12)\n
at next
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:275:10)\n
at Function.handle
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:174:3)\n
at router
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:47:12)\n
at Layer.handle [as handle_request]
(D:\PKL\cyclone-backend\node_modules\express\lib\router\layer.js:95:5)\n
at trim_prefix
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:317:13)\n
at
D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:284:7\n
at Function.process_params
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:335:12)\n
at next
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:275:10)\n
at jsonParser
(D:\PKL\cyclone-backend\node_modules\body-parser\lib\types\json.js:103:7)\n
at Layer.handle [as handle_request]
(D:\PKL\cyclone-backend\node_modules\express\lib\router\layer.js:95:5)\n
at trim_prefix
(D:\PKL\cyclone-backend\node_modules\express\lib\router\index.js:317:13)"}}
Screenshot Here
Basically 404 requests are typically due to wrong URLs entered or wrong HTTP methods. You can double check on them both and you should find the solution.
I'm using next.js with a custom express server when the params of a request contains % it causes this error:
URIError: Failed to decode param '%%faker'
at decodeURIComponent (<anonymous>)
at decode_param (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:172:12)
at Layer.match (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:148:15)
at matchLayer (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:574:18)
at next (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:220:15)
at middleware (D:\ahmed\coding\react js\with-redux-app\node_modules\http-proxy-middleware\lib\index.js:43:7)
at Layer.handle [as handle_request] (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:317:13)
at D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:335:12)
at next (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:275:10)
at expressInit (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request] (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:317:13)
at D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\ahmed\coding\react js\with-redux-app\node_modules\express\lib\router\index.js:335:12)
for example if the request is http://localhost:3000/summoner/eune/%%faker the error happens but if it's http://localhost:3000/summoner/eune/^^faker the ^^ gets encoded and the url becomes http://localhost:3000/summoner/eune/%5E%5Efaker and everything works perfectly.i could fix this error by following this answer Express handling URIError: Failed to decode param like so:
server.use((err, req, res, next) => {
if (err instanceof URIError) {
err.message = "Failed to decode param: " + req.url;
err.status = err.statusCode = 400;
console.log(err);
return res.redirect(`http://${req.get("Host")}${req.url}`);
// return app.render(req, res, "/_error");
}
});
return res.redirect(`http://${req.get("Host")}${req.url}`); this will redirect the user from http://localhost:3000/summoner/eune/%%faker to http://localhost:3000/summoner/eune/%25%25faker and if i use return app.render(req, res, "/_error"); it will send the default error page provided by next.js back to user but this is not what i want. I want to handle the % like ^.
so my questions are:
why the % doesn't get encoded to %25 and if there is a way to make it happen?
who is responsible for encoding the browser or express?
what is the best way to handle this error?
I'm using node v8.9.1, express ^4.16.3.
Please, make the answer detailed i'm a beginner developer.
Thanks for your time.
Like you pointed out, urls are percent-encoded and http://localhost:3000/summoner/eune/%%faker is just not valid as an url.
When you type an invalid url, most browsers are kind enough to change it to something valid, ex: http://localhost:3000/test test is automatically changed to http://localhost:3000/test%20test, but it's just a fallback to avoid too many errors.
In your case, % is not automatically changed to %25 because browsers cannot know when to substitute % and when to leave it. Ex: when you type %25%25faker, should this url be used as is or should it be replaced to %2525%2525faker ?
In summary: You must use valid urls at any point in time and not rely on browser kindness.
In my express application I am getting following error while getting request from user. No issue if request body is ib JSON format
UnsupportedMediaTypeError: unsupported charset "ISO-8859-1"
at urlencodedParser (/var/www/payment/node_modules/body-parser/lib/types/urlencoded.js:108:12)
at Layer.handle [as handle_request] (/var/www/payment/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/var/www/payment/node_modules/express/lib/router/index.js:317:13)
at /var/www/payment/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/var/www/payment/node_modules/express/lib/router/index.js:335:12)
at next (/var/www/payment/node_modules/express/lib/router/index.js:275:10)
at IncomingMessage.<anonymous> (/var/www/payment/gate.js:392:5)
at IncomingMessage.emit (events.js:159:13)
at endReadableNT (_stream_readable.js:1054:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11
)
I already set this in my code
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
body-parser docs say:
Returns middleware that only parses urlencoded bodies and only looks at requests where the Content-Type header matches the type option. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of gzip and deflate encodings.
I would try disabling (commenting out in your code) the bodyParser.urlencoded parser.
I wrote a demo website with nodejs. In file app.js I used express.session
Case 1:
app.use(express.cookieParser())
app.use(express.session({ secret: "whatever" }));
//this code is working
Case 2:
app.use(express.session({ secret: "whatever" }));
app.use(express.cookieParser());
//this is not correct
The error log is:
Express
500 TypeError: Cannot read property 'connect.sid' of undefined
at Object.session [as handle] (D:\web\nodejs\Weibo\node_modules\express\node_modules\connect\lib\middleware\session.js:239:32)
at next (D:\web\nodejs\Weibo\node_modules\express\node_modules\connect\lib\proto.js:193:15)
at Object.favicon [as handle] (D:\web\nodejs\Weibo\node_modules\express\node_modules\connect\lib\middleware\favicon.js:77:7)
at next (D:\web\nodejs\Weibo\node_modules\express\node_modules\connect\lib\proto.js:193:15)
at Object.handle (D:\web\nodejs\Weibo\node_modules\express-partials\index.js:94:5)
at next (D:\web\nodejs\Weibo\node_modules\express\node_modules\connect\lib\proto.js:193:15)
at Object.expressInit [as handle] (D:\web\nodejs\Weibo\node_modules\express\lib\middleware.js:30:5)
at next (D:\web\nodejs\Weibo\node_modules\express\node_modules\connect\lib\proto.js:193:15)
at Object.query [as handle] (D:\web\nodejs\Weibo\node_modules\express\node_modules\connect\lib\middleware\query.js:44:5)
at next (D:\web\nodejs\Weibo\node_modules\express\node_modules\connect\lib\proto.js:193:15)
Middleware order matter in Express, as they are executed in the order you defined them. In your case, the cookieParser middleware adds some information that is used by the session middleware.
You can check connect's documentation:
Session data is not saved in the cookie itself, however
cookies are used, so we must use the cookieParser()
middleware before session().