How to use more than one middleware in expressjs? - node.js

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.

Related

How do I make an API call inside a Firebase Cloud Function?

I'm trying to create a Firebase Cloud Function where I make an API call to NewsAPI and retrieve a set of articles. The code below is an example, and it aims to retrieve articles from BBCNews.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var url = 'http://newsapi.org/v2/top-headlines?' +
'sources=bbc-news&' +
'apiKey={myKey}';
const GetArticles = () => (
fetch(new Request(url))
.then(function (response) {
console.log(response.json());
})
);
When I run this code I get the following error message:
ReferenceError: Request is not defined
at Object.<anonymous> (C:\Kesav\Coding stuff\project-academy\functions\index.js:9:11)
at Module._compile (internal/modules/cjs/loader.js:1185:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
at Module.load (internal/modules/cjs/loader.js:1034:32)
at Function.Module._load (internal/modules/cjs/loader.js:923:14)
at Module.require (internal/modules/cjs/loader.js:1074:19)
at require (internal/modules/cjs/helpers.js:72:18)
at C:\Users\kkosa\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:661:33
at Generator.next (<anonymous>)
at fulfilled (C:\Users\kkosa\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:5:58)
How would I fix this?
You apparen't don't have any code that imports Request. That's not a "free object" in nodejs. You're going to have to find an HTTP client library for node (you have many options), import it, and use that in your code.

I just followed the step by step process on google cloud function tutorial , anyone know how to fix this? or the cause of the problem?

I only followed the step by step tutorial on google firebase
this is the link:
https://firebase.google.com/docs/functions/get-started?authuser=0
and then I get these on cmd
Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:9
exports.addMessage = functions.https.onRequest(async(req, res) => {
^
SyntaxError: Unexpected token (
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at getUserFunction (/var/tmp/worker/worker.js:439:24)
Functions deploy had errors with the following functions:
addMessage
These are the codes on the tutorial.
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest(async(req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into the Realtime Database using the Firebase Admin SDK.`enter code here`
const snapshot = await admin.database().ref('/messages').push({original:
original});
// Redirect with 303 SEE OTHER to the URL of the pushed object in the
Firebase console.
res.redirect(303, snapshot.ref.toString());
});
Looking at the code you posted compared to the code in the example, I think the problem is:
Your code:
exports.addMessage = functions.https.onRequest(async(req, res) => {
example code:
exports.addMessage = functions.https.onRequest(async (req, res) => {
Notice that in your code there is no "space" character after async. Async is a qualifier of a function that flags the function as asynchronous. See here for details of async.
The omission of the space changes the semantics. If I had to guess, the runtime would now be looking for a function that is called async. Please change your code to include a space.

Express URIError: Failed to decode param

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.

How to have service layer in loopback just like in other popular mvc frameworks?

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.

nodejs express session error

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().

Resources