当我尝试通过Express访问时,我得到一个损坏的图像链接:
app.get('/fileThumbnail', function(req, res) {
var url = proxiedURL +"?" + querystring.stringify(req.query);
logger.info('/fileThumbnail going to url', url);
request.get(url, function(err, response, img) {
logger.info("response:", response.statusCode, response.headers['content-type']);
if (!err && response.statusCode === 200) {
res.writeHead(200, {
'Content-Type': response.headers['content-type']
});
// response.pipe(res); // not working
res.writeHead(200, response.headers);
res.end(img, 'binary');
} else res.send("Error occurred:", err, "; status code: ", response.statusCode);
})
});
Run Code Online (Sandbox Code Playgroud)
缩略图来自PHP服务器:
if (!$thumbnail) {
$thumbnail = $_SERVER['DOCUMENT_ROOT'].'/images/spacer.gif';
}
// Write the thumbnail
header('Content-Type: '.image_type_to_mime_type(exif_imagetype($thumbnail)));
header('Content-length: '.filesize($thumbnail));
print file_get_contents($thumbnail,FILE_BINARY);
Run Code Online (Sandbox Code Playgroud)
尝试过几件事; 响应头如下:
Access-Control-Allow-Methods:GET,POST,PUT,DELETE,OPTIONS
access-control-allow-origin:* …Run Code Online (Sandbox Code Playgroud) 可以传递查询,但显然不是一个片段:
server.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
query: `# Welcome to GraphiQL
query PostsForAuthor {
author(id: 1) {
firstName
posts {
title
votes
}
}
}`}));
Run Code Online (Sandbox Code Playgroud)
http://dev.apollodata.com/core/fragments.html
但是,这不是原始问题的解决方案。我想在启动时将片段传递给graphiql服务器实例。
以下是 TypeScript 中的 Jest 测试。我想知道为什么需要 setImmediate() 。
第一个例子是一个有效的测试。接下来是我尝试过但不起作用的各种事情。我不明白发生了什么事。pubsub.publish 的签名是:
(method) PubSub.publish(triggerName: string, payload: any): Promise<void>
test.only('subscriptions', async () => {
const document = parse(`
subscription {
create
}
`)
const sub = <AsyncIterator<ExecutionResult>>await subscribe(schema, document);
expect(sub.next).toBeDefined()
// setInterval and process.nextTick also work here:
setImmediate(() => pubsub.publish('CREATE_ONE', { create: "FLUM!" })) // this works
const { value: { errors, data } } = await sub.next()
expect(errors).toBeUndefined()
expect(data).toBeDefined()
expect(data.create).toBe('FLUM!')
}, 10000)
Run Code Online (Sandbox Code Playgroud)
这些是我尝试过的其他事情,其中一些是在研究了类似问题的答案之后做出的。所有这些尝试都会失败,并在测试中出现超时异常:
test.only('subscriptions', async () => {
// attempt #1: jest.useFakeTimers()
const …Run Code Online (Sandbox Code Playgroud) 在版本 1 中可以正常工作,但整个服务器配置已更改。这就是我在按照 Daniel 在评论中的建议将 bodyparser() 添加到 Express 应用程序后所拥有的:
const server = new ApolloServer({
typeDefs,
resolvers,
playground: {
settings: {
'editor.theme': 'light',
}
},
})
// Initialize the app
const app = express();
app.use(cors())
app.use(bodyParser.json())
server.applyMiddleware({
app
})
app.post('/calc', function(req, res){
const {body} = req;
console.log("HOWDYHOWDYHOWDY", body) // <== body is {}
res.setHeader('content-type', 'application/json')
calculate(body)
.then(result => res.send(result))
.catch(e => res.status(400).send({error: e.toString()}))
})
Run Code Online (Sandbox Code Playgroud)
尽管调用了处理程序,但请求正文永远不会到达 app.post 处理程序。不过,我看到它是从浏览器中发出的。有任何想法吗?
更新: 丹尼尔有正确的答案,但我在使用的请求标头中遇到了另一个问题。一旦我解决了这个问题,邮件处理程序就会收到正文。
这个可能需要一个Javascript语言律师:
var s1 = "{\"x\":\"y:z\"}"
var o = JSON.parse(s1)
var s2 = JSON.stringify(o)
$('span#s').text(s1);
$('span#s2').text(s2);
if (s1 === s2) {
$('span#areEqual').text('s1 === s2')
} else {
$('span#areEqual').text('s1 !== s2')
}
JSON.parse(s2) // okay
$('span#jsonParse').text("JSON.parse(s2) okay")
eval(s2) // bad mojo!
$('span#eval').text("eval(s2) okay")
eval("("+s2+")") // bad mojo, too!
$('span#eval2').text("eval((s2)) okay")
Run Code Online (Sandbox Code Playgroud)
eval失败的s1,s2和"("+s2+")".
jsFiddle 在这里.
graphql ×2
javascript ×2
express ×1
graphiql ×1
jestjs ×1
json ×1
node.js ×1
php ×1
setimmediate ×1
typescript ×1