我有当前运行良好的index.js文件...但是为了在我的测试策略中使用,我想将其分为2个文件:server.js
和app.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.
感谢您的反馈
答案 0 :(得分:1)
您的app.js
正在导出函数,server.js
会导入该函数。但是,您实际上并没有调用该函数。在这种结构下,您的server.js
应该看起来像这样:
import app from './app';
app().listen(4000, () => {
// ....
});
也就是说,您还遇到一个问题,就是正在导出的函数app.js
实际上没有返回值,它创建了快速服务器,但没有返回它。因此,您还需要调整app.js
,方法是在方法主体的末尾添加一个返回值:
const app = express();
// ....
return app;
}
给个机会吧!
答案 1 :(得分:1)
您应该return app
在从app.js文件导出的函数中,将其导入到server.js文件中,然后以这种方式app().listen
调用该函数。您要返回的应用程序是初始化的const app = express()
函数,其中包含服务器要运行的listen()
属性。
答案 2 :(得分:0)
根据@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');
});
)