如何将我的 index.js 正确拆分为 server.js 和 app.js?

How to split correctly my index.js into server.js and app.js?

我有当前的 index.js 文件,运行 很好...但是为了在我的测试策略中使用,我想将它分成 2 个文件:server.jsapp.js

我收到一条错误消息,指出我的 app.js 不是一个函数。我的编码有什么问题吗?

index.js

import express from 'express';
import express_graphql from 'express-graphql';
import { buildSchema } from 'graphql';

// Construct a schema, using GraphQL schema language
const schema = buildSchema(`
  type Query {
    message: String
  }
`);

// Root resolver
const root = {
  message: () => 'Hello World!'
};

// Create an express server and a GraphQL endpoint
const app = express();
app.use('/graphql', express_graphql({
  schema: schema,
  rootValue: root,
  graphiql: true
}));

/* eslint-disable no-console */
app.listen(4000, () => console.log('Express GraphQL Server Now running On localhost:4000/graphql'));

拆分成:

server.js

import app from './app';

/* eslint-disable no-console */
app.listen(4000, () => {
  console.log('Express GraphQL Server Now running On localhost:4000/graphql');
});

app.js

import express_graphql from 'express-graphql';
import { buildSchema } from 'graphql';
import express from 'express';

 export default function () {

  // Construct a schema, using GraphQL schema language
  const schema = buildSchema(`
    type Query {
      message: String
    }
  `);

  // Root resolver
  const root = {
    message: () => 'Hello World!'
  };

  // Create an express server and a GraphQL endpoint
  const app = express();
  app.use('/graphql', express_graphql({
    schema: schema,
    rootValue: root,
    graphiql: true
  }));

}

console.log

yarn start
yarn run v1.9.4
$ babel-node src/server.js
/Users/yves/Developments/WIP/NODE/nodeGraphQL/src/server.js:10
_app2.default.listen(4000, () => {
              ^

TypeError: _app2.default.listen is not a function
    at Object.<anonymous> (/Users/yves/Developments/WIP/NODE/nodeGraphQL/src/server.js:4:5)
    at Module._compile (module.js:653:30)
    at loader (/Users/yves/Developments/WIP/NODE/nodeGraphQL/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/yves/Developments/WIP/NODE/nodeGraphQL/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at Object.<anonymous> (/Users/yves/Developments/WIP/NODE/nodeGraphQL/node_modules/babel-cli/lib/_babel-node.js:154:22)
    at Module._compile (module.js:653:30)
error Command failed with exit code 1.

感谢反馈

您的 app.js 正在导出一个 函数 server.js 将导入该函数。但是,您实际上并没有调用该函数。鉴于此结构,您的 server.js 需要看起来更像:

import app from './app';

app().listen(4000, () => {
    // ....
});

也就是说,您还有一个问题,即函数 app.js 导出的实际上 return 不是一个值,它创建了快速服务器但没有 return它。因此,您还需要通过在方法主体的末尾添加 return 来调整 app.js

  const app = express();
  // ....

  return app;
}

试一试!

您应该 return app 在从 app.js 文件导出的函数中,将其导入 server.js 文件,然后以这种方式调用函数 app().listen。您要返回的应用程序是初始化的 const app = express() 函数,其中包含 listen() 属性 您的服务器依赖于 运行。

根据@Elliot 和@Ekpin 回答的有效代码...(@Elliot 最先上线)

app.js

import express_graphql from 'express-graphql';
import { buildSchema } from 'graphql';
import express from 'express';

 export default function () {

  // Construct a schema, using GraphQL schema language
  const schema = buildSchema(`
    type Query {
      message: String
    }
  `);

  // Root resolver
  const root = {
    message: () => 'Hello World!'
  };

  // Create an express server and a GraphQL endpoint
  const app = express();
  app.use('/graphql', express_graphql({
    schema: schema,
    rootValue: root,
    graphiql: true
  }));

  return app;

}

server.js

    import app from './app';

    /* eslint-disable no-console */
    app().listen(4000, () => {
      console.log('Express GraphQL Server Now running On localhost:4000/graphql');
    });
)