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 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
const redisStore = koaRedis({
url: config.redisUrl
});
const app = new Koa();
app.keys = [config.secretKeyBase];
// not serve static when deploy
if(config.serveStatic){
app.use(convert(require('koa-static')(__dirname + '/../public')));
}
app.use(convert(session({
store: redisStore,
prefix: 'kails:sess:',
key: 'kails.sid'
})));
app.use(cacheMiddle());
app.use(bodyParser());
app.use(methodOverride((req, _res) => {
if (req.body && (typeof req.body === 'object') && ('_method' in req.body)) {
// look in urlencoded POST bodies and delete it
const method = req.body._method;
delete req.body._method;
return method;
}
}));
import render from 'core/universal-render'
import { PORT } from 'core/config'
const { NODE_ENV } = process.env
const app = new Koa()
debug.enable('koa')
// Proxy asset folder to webpack development server in development mode
if (NODE_ENV === 'development') {
const proxy = require('koa-proxy')({
host: `http://0.0.0.0:${PORT + 1}`,
map: (filePath) => `assets/${filePath}`
})
app.use(convert(mount('/assets', proxy)))
} else {
const cacheOpts = { maxAge: 86400000, gzip: true }
app.use(convert(mount('/assets', staticCache(resolve('./dist'), cacheOpts))))
}
app.use(async (ctx) => {
try {
ctx.status = 200
ctx.body = await render({
assets: require('./webpack-stats.json'),
store: createStore(),
location: ctx.request.url
})
// Don't cache assets name on dev
if (NODE_ENV === 'development') {