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.
Related
I am trying to use 2 middlewares for my route, auth.js and authAdmin.js which are in different folder.
var middleware = [auth, authAdmin]
router.get("/all_infor", middleware, userCtrl.getUsersAllInfor)
Official page of Expressjs defined the same way I did above but I am getting error:
F:\Mern projects\full_auth\node_modules\express\lib\router\route.js:202
throw new Error(msg);
^
Error: Route.get() requires a callback function but got a [object Object]
at Route.<computed> [as get] (F:\Mern
projects\full_auth\node_modules\express\lib\router\route.js:202:15)
at Function.proto.<computed> [as get] (F:\Mern
projects\full_auth\node_modules\express\lib\router\index.js:510:19)
at Object.<anonymous> (F:\Mern projects\full_auth\routes\userRouter.js:25:8)
can any one suggest how do I fix this? Thank You in advance.
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
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
I have started building an application in sailsjs but I decided to move it to loopback. From a j2ee/spring mvc background I was quickly up and running with sailsjs with some of my business logic in the api/service.
Unfortunatly I have not found a way to create those services on loopback. I am not talking about remote method. These services are not really tied to any model, they are on a layer above models. I have tried creating the following at server/service/DataModelService.js
module.exports = {
testMethod: function(){
return "Hello joseph"
},
testAnotherMethod: function(req,res){
//lots of other processing etc. Calling other services etc
res.send("something")
}
}
Created server/boo/routes.js with this following
module.exports = function(app){
app.get('/test', function(req, res){
res.send(DataModelService.testMethod());
});
}
but quickly got this reference error:
DataModelService is not defined
at /media/joseph/Data/Personal/tutorials/testingloopback/server /boot/routes.js:3:18
at Layer.handle [as handle_request] (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/layer.js:95:5)
at next (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/layer.js:95:5)
at /media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/index.js:330:12)
at next (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/loopback/node_modules/express/lib/router/index.js:271:10)
at cors (/media/joseph/Data/Personal/tutorials/testingloopback/node_modules/cors/lib/index.js:178:7)
at /media/joseph/Data/Personal/tutorials/testingloopback/node_modules /cors/lib/index.js:228:17
Can anyone show the right way of doing this ?
Thanks in advance
You need to require the module you're trying to access. Try this:
// server/boot/routes.js
var DataModelService = require('../service/DataModelService.js');
module.exports = function(app){
app.get('/test', function(req, res){
res.send(DataModelService.testMethod());
});
};
Without the require() call the variable is undefined. You can require this service (which is just a plain Node module) in any file in your application this way.
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().