Reputation: 21405
I am new to express and node, I working on a task where I want to add some json data to middleware request, here is the approach I am following:
In my middleware I want to add some details to request like current date and time and then extract the URL path. Here is the code I came up with:
var express = require('express');
var app = express();
module.exports = app.use('/some-url', function(req, res, next) {
let logdetails = {
id: "some-id",
datetime: Date.now(),
path: '/path-in-url'
}
req.logdetails= logdetails;
next();
} );
I am using module.exports
so it this function is exported. But it is giving errors. Also what is the correct way to access the URL path, for example if URL is /users/john
then I want to extract /john
and my middleware should be applied only when URL starts with /users
.
Upvotes: 1
Views: 1175
Reputation: 272
module.exports = {
handler: (req, res, next) => {
const userName = req.params.name;
// add payload to request
next();
}
}
middleware = require("middleware");
app.use('/users/:name', middleware.handler);
Concerning URL path access you could get access using request params
, e.g
app.use('/users/:name', (req, res, next) => {
const userName = req.params.name;
// add payload to request
next();
});
1. Register your specific URL - `/users/:name`, where `:name` is dynamic params e.g *john*
2. Grab params in the callback
Upvotes: 1
Reputation: 86
req.originalUrl returns the url path like shown below
// GET /search?q=something
console.dir(req.originalUrl)
// => '/search?q=something'
source: https://expressjs.com/en/api.html
Similarly, get the URL path and split the string accordingly and apply the condition you need.
Here if your originalPath is /users/john then
const result = "/users/john".split("/")
would result in ["", "users", "john"]
Check if the result[1]==="users"
and write your condition in the middleware.
You don't have to export the middle ware.
Upvotes: 1
Reputation: 12542
If you want the middleware then just export the middleware function. Not the Whole app.use
part. I think app.use
returns an Express object. And :param
in your url will make sure that you can access that parameter. You can check out more about path patterns here
middleware.js
module.exports = function(req, res, next) {
let logdetails = {
id: "some-id",
datetime: Date.now(),
path: '/path-in-url'
}
req.logdetails= logdetails;
next();
}
Your other main.js file:
const express = require('express');
const app = express();
const middleWare = require('./middleware')
app.use('/users/:name', middleWare, (req, res)=>{
//you can access logDetails
// and the name
const name = req.params.name;//This will be john for /users/john
const logDetails = req.logdetails;
})
Upvotes: 1
Reputation: 29282
Also what is the correct way to access the URL path, for example if URL is /users/john then I want to extract /john
If your request url is /users/john
then req.path
will give you the path of the request url, i.e. /users/john
. To extract john, you can use named route parameters and define your route as /users/:name
. After using named route parameter in your route, if your request url is /users/john
, req.params.name
will give you the route parameter, i.e. john. For more details, take a look at req.params on express docs
and my middleware should be applied only when URL starts with /users
following middleware will only run when request path is /users
app.use('/users', (req, res, next) => {
...
}
Upvotes: 2