我有与此完全相同的问题:What's the most efficient way to call a Node.js backend function with JavaScript
除了找不到Koa和Next JS构建的服务器(不是Express)的等效解决方案。
我有一个后端函数,需要从前端Javascript文件调用-该函数需要在后端运行。
代码:(需要运行提取功能)
const Koa = require('koa');
const next = require('next');
const session = require('koa-session');
const port = parseInt(process.env.PORT, 10) || 8080;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
function fetch(){
storage
.getBuckets()
.then((results) => {
const buckets = results[0];
console.log('Buckets:');
buckets.forEach((bucket) => {
console.log(bucket.name);
});
})
.catch((err) => {
console.error('ERROR:', err);
});
}
app.prepare().then(() => {
const server = new Koa();
server.use(session(server));
server.use(async (ctx) => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
ctx.res.statusCode = 200;
return
});
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
});