Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
},
formatError: (error: GraphQLError): any => {
console.log(error.message);
console.log(error.locations);
console.log(error.stack);
return {
message: error.message,
locations: error.locations,
stack: error.stack
};
}
};
};
const graphqlServer = convert(graphqlHttp(graphqlSettingsPerReq));
app.prepare().then(() => {
const server = new Koa();
const router = new Router();
router.get('/', async ctx => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
});
// router.get('/post/:slug', async ctx => {
// console.log('========== /post', ctx.params, ctx.query);
// // await handle(ctx.req, ctx.res, { query: { slug: ctx.params.slug } });
// await app.render(ctx.req, ctx.res, '/post', { slug: ctx.params.slug });
// ctx.respond = false;
// });
global.pwd = __dirname;
const app = new Koa();
const httpPort = baseConfig.httpPort;
const httpsPort = baseConfig.httpsPort;
const options = {
key: fs.readFileSync(path.join(__dirname, './pem/privatekey.pem')),
cert: fs.readFileSync(path.join(__dirname, './pem/certificate.pem'))
};
// middleware
app.use(Session());
app.use(response);
app.use(convert(KoaBodyParser()));
app.use(convert(cors()));
app.use(Views(path.join(__dirname, './views')));
app.use(convert(Static(path.join(__dirname, 'static'))));
app.use(convert(Static(path.join(__dirname, 'upload'))));
// use all route
for (var item of routers) {
app.use(item.routes(), item.allowedMethods());
}
// err handler
app.on('error', (err, ctx) => {
console.error('server error', err, ctx);
});
app.listen(httpPort);
https.createServer(options, app.callback()).listen(httpsPort);
export default function middleware() {
return convert.compose(
logger(),
bodyParser(),
compress({
filter: function (content_type) {
if (/event-stream/i.test(content_type)) {
// 为了让hot reload生效,不对__webpack_hmr压缩
return false;
} else {
return true;
}
},
})
);
}
import cros from './middleware/crosMiddleware'
import pipeMiddleware from './middleware/pipeMiddleware'
import routing from './routes/'
import { port, mongodb } from './config'
mongoose.connect(mongodb)
mongoose.connection.on('error', console.error)
const app = new Koa()
app.jsonSpaces = 0 // 压缩json返回中的空格
app.keys = ['key']
app.use(convert.compose(
cros, // 跨域
logger(),
bodyParser(),
session(app),
pipeMiddleware()
))
// app.use(cros) // 跨域
// app.use(convert(logger()))
// app.use(convert(bodyParser()))
// app.use(convert(session(app)))
// app.use(pipeMiddleware())
routing(app)
app.listen(port, () => console.log(`✅ The server is running at http://localhost:${port}/`))
export default function middleware() {
return compose([
logger(),
convert(cors()),
convert(bodyParser()),
// convert(session()),
]);
}
let renderer
const createRenderer = fs => {
const bundlePath = path.resolve(process.cwd(), 'build/server-bundle.js')
return createBundleRenderer(fs.readFileSync(bundlePath, 'utf-8'))
}
const app = new Koa()
if (process.env.NODE_ENV === 'development') {
const webpack = require('webpack')
const webpackConfig = require('../scripts/webpack.client.dev')
const compiler = webpack(webpackConfig)
const devMiddleware = require('koa-webpack-dev-middleware')
const hotMiddleware = require('koa-webpack-hot-middleware')
app.use(convert(devMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
stats: {
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
}
})))
app.use(convert(hotMiddleware(compiler)))
// server renderer
const serverBundleConfig = require('../scripts/webpack.bundle')
const serverBundleCompiler = webpack(serverBundleConfig)
const mfs = new MFS()
serverBundleCompiler.outputFileSystem = mfs
// these files. This middleware doesn't need to be enabled outside
// of development since this directory will be copied into ~/dist
// when the application is compiled.
app.use(convert(serve(paths.client('static'))));
} else {
debug(
'Server is being run outside of live development mode. This starter kit ' +
'does not provide any production-ready server functionality. To learn ' +
'more about deployment strategies, check out the "deployment" section ' +
'in the README.'
);
// Serving ~/dist by default. Ideally these files should be served by
// the web server and not the app server, but this helps to demo the
// server in production.
app.use(convert(serve(paths.base(config.dir_dist))));
}
export default app;
const xml = feed.xml({ indent: true });
ctx.type = 'application/rss+xml; charset=utf-8';
ctx.body = xml;
ctx.sendUAEvent('backend', 'getRealTimeFeed');
});
// === Server initialization ===
const root = new koa();
root.name = app.name;
root.proxy = true;
root.use(convert(cors()));
root.use(convert(app.log.getLoggerMiddleware()));
root.use(router.routes());
root.use(router.allowedMethods());
root.listen(this.port, () => {
app.log.info('Listening on port ' + this.port);
});
}
}
Object.keys(proxies).forEach((key) => {
const from = new RegExp(key);
const to = proxies[key];
// eslint-disable-next-line
const { host, protocol } = url.parse(to);
app.use(convert(proxy({
host: `${protocol}//${host}`,
jar: true,
match: from,
map: endpoint => endpoint.replace(from, to)
})));
});
}
export default function app (config, webpackConfig) {
let app = new Koa()
const paths = config.utils_paths
// Enable koa-proxy if it has been enabled in the config.
if (config.proxy && config.proxy.enabled) {
app.use(convert(proxy(config.proxy.options)))
}
// This rewrites all routes requests to the root /index.html file
// (ignoring file requests). If you want to implement isomorphic
// rendering, you'll want to remove this middleware.
app.use(convert(historyApiFallback({
verbose: false
})))
// ------------------------------------
// Apply Webpack HMR Middleware
// ------------------------------------
if (config.env === 'development') {
const compiler = webpack(webpackConfig)
// Enable webpack-dev and webpack-hot middleware
const { publicPath } = webpackConfig.output
app.use(webpackDevMiddleware(compiler, config, publicPath))
app.use(webpackHMRMiddleware(compiler))
// Serve static assets from ~/src/static since Webpack is unaware of