Closed sachinsawant71 closed 5 years ago
That middleware needs to be added before any service (before app.configure(services)
).
The middleware is before any service as mentioned by you.
I have turned on socket.io. Is it causing any issues?
Are you trying to access this data in a REST request or a WebSocket request?
REST service. I am accessing it in a REST service
Then it should work. I tried the same middleware you provided in the feathers-chat and it worked as expected. Please provide a complete example to reproduce the issue if that still isn't the case for you.
Here is my app.js file. I tried to access feathers.sample in hook, but the object does not exist.
const path = require('path');
const fs = require('fs');
const favicon = require('serve-favicon');
const compress = require('compression');
const helmet = require('helmet');
const cors = require('cors');
const logger = require('./helpers/logger');
const feathers = require('@feathersjs/feathers');
const configuration = require('@feathersjs/configuration');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');
const handler = require('@feathersjs/express/errors');
const notFound = require('@feathersjs/errors/not-found');
const middleware = require('./middleware');
const services = require('./services');
const appHooks = require('./app.hooks');
const channels = require('./channels');
const bodyParser = require('body-parser');
const createJobManagerInstance = require('./jobManager').createInstance;
const swagger = require('feathers-swagger');
const swOptions = require('./swagger/swagger');
const seed = require('./seed');
const scripts = require('./scripts');
const authentication = require('./authentication');
const mongoose = require('./mongoose');
const def = require('./swagger/authentication');
require('dotenv').config();
const env = process.env.NODE_ENV || 'dev';
const LOG_DIR = 'logs';
const numCPUs = require('os').cpus().length;
// Create the log directory if it does not exist
if (!fs.existsSync(LOG_DIR)) {
fs.mkdirSync(LOG_DIR);
}
const app = express(feathers());
// Load app configuration
app.configure(configuration());
// Enable security, CORS, compression, favicon and body parsing
app.use(helmet());
app.use(cors());
app.use(compress());
app.use(bodyParser.json({ limit: '2mb' }));
app.use(bodyParser.urlencoded({ limit: '2mb', extended: true }));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(favicon(path.join(app.get('public'), 'favicon.ico')));
// Host the public folder
app.use('/', express.static(app.get('public')));
app.use('/static', express.static(__dirname + '/public/static'));
// Set up Plugins and providers
app.configure(express.rest());
app.configure(socketio());
// Configure Swagger
app.configure(swagger(swOptions));
// Tell mongoose to use native promises
mongoose.Promise = global.Promise;
app.configure(mongoose);
// Configure other middleware (see `middleware/index.js`)
app.configure(middleware);
authentication.docs = def;
app.use((req, res, next) => {
req.feathers.ip = req.connection.remoteAddress;
req.feathers.userAgent = req.get('user-agent');
req.feathers.headers = req.headers;
req.feathers.sample = "SampleWork";
next();
});
app.configure(authentication);
// Set up our services (see `services/index.js`)
app.configure(services);
// Set up event channels (see channels.js)
app.configure(channels);
//seed initiatl data
app.configure(seed);
//run scripts to update data
app.configure(scripts);
// // handle every other route with index.html, which will contain
// // a script tag to your application's JavaScript file(s).
app.get('*', function (request, response) {
response.sendFile(path.resolve(__dirname, '../public', 'index.html'))
});
// Configure a middleware for 404s and the error handler
app.use(notFound());
app.use(handler());
app.hooks(appHooks);
module.exports = app;
It's set as context.params.sample
(context.params.userAgent
, context.params.ip
) in a hook (documented here).
I checked the context object in my hook function,
I get following objects -
But the objects I set as a part of middle ware above e.g. userAgent, ip etc are missing. Not sure is anything else is missing out here.
Like I mentioned, please provide a repository with a complete example to reproduce your issue. I verified again that in a standard setup like the chat application this is working as expected.
Thanks for your inputs. Let me investigate it further on my side. If I need help, I will open a new ticket with example code.
Closing the ticket.
you can use middleware instead, it gives you complete express request object
Steps to reproduce
I registered following middleware in my app.js file app.use((req, res, next) => { req.feathers.ip = req.connection.remoteAddress; req.feathers.userAgent = req.get('user-agent'); req.feathers.headers = req.headers; next(); });
Expected behavior
as per documentation I should get ip, userAgent, headers field in "params" object
Actual behavior
params object in hook does not contain the fields I set in the middleware
System configuration
FeathersJs versions : 3.1.5
NodeJS version: 8.10
Operating System: Windows