没有报错,但是在浏览器访问不到?
来源:4-2 构建初始项目首次验证公众号服务器
拖车板牙爵士
2017-08-04
start.js内容如下:
require('babel-core/register')({ 'presets': [ 'stage-3', 'latest-node' ] }) require('babel-polyfill') require('./server')
但是控制台报错404
package.json中的配置:
"scripts": { "dev": "nodemon -w ./server -w ./start.js --exec node ./start.js", "build": "nuxt build && backpack build", "start": "cross-env NODE_ENV=production node build/main.js", "precommit": "npm run lint", "lint": "eslint --ext .js,.vue --ignore-path .gitignore ." }
server下的index.js内容如下:
import Koa from 'koa' import { Nuxt, Builder } from 'nuxt' import R from 'ramda' import { resolve } from 'path' // Import and Set Nuxt.js options let config = require('../nuxt.config.js') config.dev = !(process.env === 'production') const r = path => resolve(__dirname,path) const host = process.env.HOST || '127.0.0.1' const port = process.env.PORT || 3000 const MIDDLEWARES = ['router']; class Server { constructor () { this.app = new Koa(); this.useMiddleWares(this.app)(MIDDLEWARES); } useMiddleWares (app) { return R.map(R.compose( R.map(i => i(app)), require, i => `${r('./middlewares')}/${i}`) ) } async start () { // Instantiate nuxt.js const nuxt = new Nuxt(config) // Build in development if (config.dev) { const builder = new Builder(nuxt) builder.build().catch(e => { console.error(e) // eslint-disable-line no-console process.exit(1) }) } this.app.use(ctx => { ctx.status = 200 // koa defaults to 404 when it sees that status is unset return new Promise((resolve, reject) => { ctx.res.on('close', resolve) ctx.res.on('finish', resolve) nuxt.render(ctx.req, ctx.res, promise => { // nuxt.render passes a rejected promise into callback on error. promise.then(resolve).catch(reject) }) }) }) this.app.listen(port, host) console.log('Server listening on ' + host + ':' + port) // eslint-disable-line no-console } } const app = new Server(); app.start();
写回答
2回答
-
Scott
2017-08-07
代码参考这个试下:
const app = new Koa() const useMiddleware = (app) => { return R.map(R.compose( R.map(i => i(app)), require, i => `${r('./middleware')}/${i}`) ) } const start = async () => { let config = require('../nuxt.config.js') config.dev = !(app.env === 'production') const nuxt = await new Nuxt(config) if (conf.env !== 'production') { try { await nuxt.build() } catch (e) { console.error(e) process.exit(1) } } await useMiddleware(app)(MIDDLEWARE) app.use(async (ctx, next) => { ctx.status = 200 await nuxt.render(ctx.req, ctx.res) }) app.listen(conf.port, conf.host) console.log('Server listening on ' + conf.host + ':' + conf.port) // eslint-disable-line no-console } start()
012017-08-08 -
Scott
2017-08-06
server.js 里面是怎么写的,是不是没有返回任何内容,导致 404 了
032017-08-07
相似问题