无法通过中间件检索添加到req对象的值

时间:2019-03-08 07:51:09

标签: node.js express middleware apollo-server

我的服务器配置如下:

import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import { typeDefs, resolvers } from './schema';
import playgroundSettings from './playground.json';
import isAuth from './middleware/isAuth';


// GraphQL: Schema
const server = new ApolloServer({
  typeDefs,
  resolvers,
  playground: {
    endpoint: '/blog', /* populates the playground endpoint box below the tab-strip. */
    settings: playgroundSettings,
  },
});

// Express: Initialize
const app = express();
// Middleware: isAuth
app.use(isAuth);
// Middleware: GraphQL route
server.applyMiddleware({
  app,
  path: '/',
});
// Express: Listener
app.listen(process.env.GRAPH_BLOG_PORT, () => {
  console.log(`Server started on port: ${process.env.GRAPH_BLOG_PORT}`);
});

// Exports
export default app;

这是使用Apollo Server 2.0和ExpressJS的GraphQL实现。但是,您可以看到,在使用GraphQL路由之前,我正在使用自定义中间件执行一些授权任务。该中间件名为isAuth.js,并按预期方式被调用和执行。但是,我以某种方式无法从解析器检索此中间件添加到我的req对象的属性。中间件看起来像这样:

// isAuth.js
import jwt from 'jsonwebtoken';

module.exports = (req, res, next) => {
  let decodedToken;
  const authHeader = req.get('Authorization');
  if (!authHeader) {
    req.isAuth = false;
    return next();
  }
  const token = authHeader.split(' ')[1];
  if (!token || token === '') {
    req.isAuth = false;
    return next();
  }
  try {
    decodedToken = jwt.verify(token, process.env.GRAPH_BLOG_JWT_SECRET);
  } catch (err) {
    req.isAuth = false;
    return next();
  }
  if (!decodedToken) {
    req.isAuth = false;
    return next();
  }
  req.isAuth = true;
  req._id = decodedToken._id;
  return next();
};

当我在解析器中执行console.log(req);时,我希望使用诸如isAuth_id之类的属性,但是可以得到以下内容:

{ _extensionStack:
   GraphQLExtensionStack {
     extensions: [ [FormatErrorExtension], [CacheControlExtension] ] } }

这是怎么回事?

1 个答案:

答案 0 :(得分:0)

您需要添加上下文行,如下所示:

 (node:4704) [DEP0018] DeprecationWarning: Unhandled promise rejections 
  are deprecated. In the future, promise rejections that are not handled 
  will terminate the Node.js process with a non-zero exit code.
  [1] events.js:177
  [1]       throw er; // Unhandled 'error' event
  [1]       ^
  [1] 
  [1] Error: getaddrinfo ENOTFOUND https://3.13.108.243
  [1]     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)
  [1] Emitted 'error' event at:
  [1] at /home/node_modules/mysql2/lib/pool.js:143:20
  [1] at /home/node_modules/mysql2/lib/pool.js:59: 18
  [1] at PoolConnection.<anonymous (/home/node_modules/mysql2/lib/connection.js:716:13)
  [1] at Object.onceWrapper (events.js:288:20)
  [1] at PoolConnection.emit (events.js:205:15)
  [1] at PoolConnection._notifyError (/home/node_modules/mysql2/lib/connection.js:212:12)
  [1] at PoolConnection._handleFatalError (/home/node_modules/mysql2/lib/connection.js:151:10)
  [1] at PoolConnection._handleNetworkError (/home/node_modules/mysql2/lib/connection.js:160:10)
  [1]     at Socket.emit (events.js:200:13)
  [1]     at emitErrorNT (internal/streams/destroy.js:91:8) {
  [1]   errno: 'ENOTFOUND',
  [1]   code: 'ENOTFOUND',
  [1]   syscall: 'getaddrinfo',
  [1]   hostname: 'MyIpInstance',
  [1]   fatal: true
  [1] }