I'm using Express with Node.js and am quite confused about refreshing behavior. Upon refresh of /test, it seems like something is cached server-side when it hits app.use because the array length is nonzero (see sample code below). I would expect the array length to reset to zero since I'm hitting /testagain when I'm refreshing the browser.
Does app.use cache things by default? How does Express middleware work in terms of refresh? I couldn't find anything that explained this clearly in the Express 4.14 docs.
==================
Browser Endpoint: localhost:8000/test
Client:
$.get('/data').done(function(response){...}
Route:
module.exports = function(app) {
app.get('/test', function(req,res) {
var arr =[];
app.use('/data', function(req,res, next) {
console.log(arr.length); // this is nonzero on refresh
arr.push(/* some stuff*/);
res.send({"arr": arr});
}
res.render('test.html')
}
}
Server:
var express = require('express');
var app = express();
require('./routes/route')(app);
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
var server = app.listen(8000, function() {
console.log("server started 8000");
});
app.use(express.static(__dirname + '/public'));
It's not really server caching. It's because you are registering middleware inside a closure and thus those closure variables (like arr) are retained for the next invocation of the middleware. In addition, you're registering the middleware over and over (a new one each time /test is hit).
When you register app.use() inside an app.get() that means that every time you hit the /test route, you will add another duplicate app.use() handler. They will accumulate over time and the same middleware will get run multiple times for for the same request, retaining the previous value for arr from when it was originally registered, but each with their own value for that array.
The general solution here is to NOT register app.use() inside of app.get() because you only want one handler - you don't want them to accumulate.
It's unclear what you're trying to accomplish with your app.use('/data/, ...) middleware. It is clear that your current structure is wrong, but without understanding what you were trying to do with that, it's not clear exactly how it should be written. The usual function of middleware is to be registered once during the initialization of the server and never inside a request handler.
If you're trying to respond to your ajax call:
$.get('/data').done(function(response){...}
Then, you would want an app.get('/data', ...) at the top level of your app module to make that work.
Please explain what the arr.push() is supposed to accomplish for us to help in any more detail.
Related
I am using Expressjs and the Auth0 API for authentication and ReactJs for client side.
Because of the limitations of the Auth0 API (spoke with their team) I am sending updated user details to my backend and then using app.set() to be able to use the req.body in another route.
I need to call the app.patch() route automatically after the app.post() route has been hit.
The end goal is that the users data will be updated and shown client side.
const express = require('express');
const cors = require('cors');
const path = require('path');
const app = express();
require('dotenv').config()
const { auth } = require("express-openid-connect");
app.use(express.json());
app.use(cors());
app.use(express.static(path.join(__dirname, 'build')));
app.use(
auth({
issuerBaseURL: process.env.AUTH0_ISSUER_BASE_URL,
baseURL: process.env.BASE_URL,
clientID: process.env.AUTH0_CLIENT_ID,
secret: process.env.SESSION_SECRET,
authRequired: false,
auth0Logout: true,
})
);
app.get('/', async (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.get('/api', async (req, res) => {
const stripe = require('stripe')(`${process.env.REACT_APP_Stripe_Live}`);
const invoice = await stripe.invoices.list({
limit: 3,
});
res.json(invoice);
});
app.post('/updateuser', (req, ) => {
app.set('data', req.body);
})
app.patch(`https://${process.env.AUTH0_ISSUER_BASE_URL}/api/v2/users/:id`,(req,res) => {
let val = app.get('data');
req.params = {id: val.id};
console.log(req.params);
})
app.listen(process.env.PORT || 8080, () => {
console.log(`Server listening on 8080`);
});
I'd suggest you just take the code from inside of app.patch() and make it into a reusable function. Then it can be called from either the app.patch() route directly or from your other route that wants to do the same funtionality. Just decide what interface for that function will work for both, make it a separate function and then you can call it from both places.
For some reason (which I don't really understand, but seems to happen to lots of people), people forget that the code inside of routes can also be put into functions and shared just like any other Javascript code. I guess people seems to think of a route as a fixed unit by itself and forget that it can still be broken down into components and those components shared with other code.
Warning. On another point. This comment of yours sounds very wrong:
and then using app.set() to be able to use the req.body in another route
req.body belongs to one particular user. app.set() is global to your server (all user's requests access it). So, you're trying to store temporary state for one single user in essentially a global. That means that multiple user's request that happen to be in the process of doing something similar will trounce/overwrite each other's data. Or worse, one user's data will accidentally become some other user's data. You cannot program a multi-user server this way at all.
The usual way around this is to either 1) redesign the process so you don't have to save state on the server (stateless operations are generally better, if possible) or 2) Use a user-specific session (like with express-session) and save the temporary state in the user's session. Then, it is saved separately for each user and one user's state won't overwrite anothers.
If this usage of app.set() was to solve the original problem of executing a .patch() route, then the problem is solved by just calling a shared function and passing the req.body data directly to that shared function. Then, you don't have to stuff it away somewhere so a later route can use it. You just execute the functionality you want and pass it the desired data.
Does using app.use(express.static("public")) call the middleware for every request, even if it wasn't a request for a static resource?
It will only get called if a route hasn't dealt with the request already.
Keeping in mind that routes are tested in the order they are registered, take this example:
const express = require('express');
const app = express();
const port = 3000;
app.get('/foo', (req, res) => {
console.log('Foo!');
res.send('Foo!');
});
app.use(function (req, res, next) {
console.log('middleware triggered');
next();
});
app.get('/bar', (req, res) => {
console.log('Bar!');
res.send('Bar!');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
If I request http://localhost:3000/foo then the server will log:
Foo!
The /foo endpoint matched the request and then called res.send().
If I request http://localhost:3000/bar then it logs:
middleware triggered
Bar!
The middleware kicks in (because it matches the route), called next() to go to the next function that matches the route, and then the /bar handler is called.
It is important to position your static middleware carefully.
If you put it before the route you want to match the request then there are two possible negative effects:
You'll call it when it isn't needed which is inefficient
A static file will match a route instead of an actual route handler
On the other hand, if you put it last then you'll solve the efficiency problem, but some bad route design might mean that something creates a URL which matches an already existing static file and masks it.
It's a good idea to specify a directory that you know will never conflict with a route (e.g. app.use('/static', express.static('public'));) to avoid that possibility. As a bonus it means that any broken links which would normally 404 won't have to go through the static middleware unless the link is pointing in the /static path in the first place.
When registering it like that it, the middleware will run on every request, yes.
Basically because that statement is actually the same as:
app.use("/", express.static("public"))
Calling express.static returns a classic middleware function that will be run on every path you specify in app.use.
If you want it only to kick in on a specific path, you could register it like this:
app.use('/static', express.static('public'));
My middlewares are getting called multiple times and I can't figure out why.
It's a really short code and it is very frustrating as I just started learning express and node.
I don't understand why it's even getting into the second middleware, I didn't use next(), I used res.send().
I am taking an online course and it's the same code as it is described. I also searched stackoverflow but nothing helped.
I did read something about the favicon that calls it a second time, but I can't figure out why this is getting called multiple times.
const express = require("express");
const app = express();
app.use("/", (req, res, next) => {
console.log("This always runs!");
next();
});
app.use("/add-product", (req, res, next) => {
console.log("In first middleware!");
res.send("<h1>Add Product</h1>");
});
app.use("/", (req, res, next) => {
console.log("In second middleware!");
res.send("<h1>Hello from express!</h1>");
});
app.listen(3000);
If I'm opening localhost:3000/add-product I should get in the console:
This always runs!
In first middleware!
but I actually get:
This always runs!
In first middleware!
This always runs!
In second middleware!
This always runs!
In first middleware!
Could it be that the favicon automatically executes all middlewares once? I added this code before the first app.use() call:
app.get("/favicon.ico", (req, res) => res.status(204));
Now I get
This always runs!
In first middleware!
This always runs!
In first Middleware!
I still get it twice.
edit edit edit:
This appears only to happen in chrome.
Don't use app.use for routes that's mainly used for middleware registration you want to use the router. https://expressjs.com/en/4x/api.html#app.use
app.(post|get|delete|put)("route", function(req,res,next){})
in your case it's best to see if your browser is requesting 2 http calls. If so it'll double up.
The browser is sending a request for a favicon. Press F12 on chrome and you will see two requests being made. One of them will be local host and other one will be favicon.ico. Therefore you are seeing two sets of console.log() being printed out to the console.
Instead of reinstalling chrome you could just right click on favicon.ico and click on Block request URL to stop the browser from sending requests for favicon.ico
Was having the same kind of problem but I didn't find any solution online, but I think I found the solution for this. The reason the middleware is being run more than once is that the client is making a request to the server more than once and each of those requests needs to go through the middleware each time.
For example, the first request is the route request itself, then the second one is the request for stuff in public such as images, CSS files, and more. So 2 requests are being made and that means the middleware will be run twice.
My code solution for this is just to have an if statement that checks whether the middleware req.originalUrl contains the file or folder name that is in public, if it doesn't contain it then the middleware code would then be run.
function middleware(req, res, next) {
const condition = !req.originalUrl.includes("css") && !req.originalUrl.includes("image")
if (condition) {
// middleware code
} else if (!condition) {
next()
}
}
I would only suggest you to do this only if you are making a middleware such as a page counter, or a middleware that is only needed to be run once per route request
Problem solved: I reinstalled Chrome and it works fine now. Thanks all!
I have recently started a new app with Express 4.0, and I really liked the new express.Router() thing.
Something that got me stuck for quite some time was the following behavior:
var app = require('express')(),
router = express.Router();
router.get('/me', function(req, res, next) {
if(someBoolean) {
return next(); //here I expect to get a 404 error from express!
}
res.json({name: 'yourName'});
});
router.get('/:user_id', function() {
//any code here!
});
app.use('/users', router);
I always used next() (in Express 3.x) to either call the next middleware or to force a 404 from the server.
Since /users/me and /users/45 are totally different routes and are not mounted in a way that one should come after another, I wonder why I get code in /users/:user_id evaluated after calling next() in /users/me.
Am I doing something wrong or things are supposed to work different in Express 4.0?
This is working as expected. In Express 4 all middleware/routes are executed in the order that they are added to your app, so the path for your second router.get() is tested and matches because :user_id could be any kind of token, numeric or otherwise (basically any character that isn't /).
You can fix this at least in a couple of different ways:
Don't call next() in your first router.get()
Use a RegExp instead of a string to ensure that user_id only matches a numeric value. For example: router.get(/^\/(\d+)$/, function() {. However then you have to access the param via regexp group index instead of by a name: var user_id = req.params[0].
I'm creating a simple testing platform for an app and have the following code setup as my server.js file in the root of my app:
var restify = require('restify'),
nstatic = require('node-static'),
fs = require('fs'),
data = __dirname + '/data.json',
server = restify.createServer();
// Serve static files
var file = new nstatic.Server('');
server.get(/^\/.*/, function(req, res, next) {
file.serve(req, res, next);
});
// Process GET
server.get('/api/:id', function(req, res) {
// NEVER FIRES
});
It serves static files perfectly, however, when I try to make a call to the /api it just hangs and times out. Imagine I'm missing something stupid here, any help would be greatly appreciated.
node-static is calling next with an error, which means it's never yielding to other handlers.
You can move your other handlers above node-static or ignore it's errors by intercepting it's callback.
I made a working version here: http://runnable.com/UWXHRONG7r1zAADe
You may make yourself sure the api get call is caught by moving the second get before the first. The reason is your api calls routes are already matched by the first pattern.