Hello world!
')) t.is(context.nuxt.error, null) t.is(context.nuxt.data[0].name, 'world') diff --git a/lib/build/index.js b/lib/build/index.js index 2e0813c0ba..4f211f5184 100644 --- a/lib/build/index.js +++ b/lib/build/index.js @@ -52,6 +52,10 @@ module.exports = function * () { if (noBuild) { const serverConfig = getWebpackServerConfig.call(this) const bundlePath = join(serverConfig.output.path, serverConfig.output.filename) + if (!fs.existsSync(bundlePath)) { + console.error('> No build files found, please run `nuxt build` before launching `nuxt start`') + process.exit(1) + } createRenderer.call(this, fs.readFileSync(bundlePath, 'utf8')) return Promise.resolve() } @@ -64,15 +68,15 @@ module.exports = function * () { } else { console.error('> Couldn\'t find a `pages` directory. Please create one under the project root') } - process.exit() + process.exit(1) } if (this.options.store && !fs.existsSync(join(this.dir, 'store'))) { console.error('> No `store` directory found (store option activated). Please create on under the project root') - process.exit() + process.exit(1) } if (this.options.store && !fs.existsSync(join(this.dir, 'store', 'index.js'))) { console.error('> No `store/index.js` file found (store option activated). Please create the file.') - process.exit() + process.exit(1) } debug(`App root: ${this.dir}`) debug('Generating .nuxt/ files...') diff --git a/lib/nuxt.js b/lib/nuxt.js index 92014d4dbd..cb704eaacd 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -44,8 +44,11 @@ class Nuxt { }) // renderer used by Vue.js (via createBundleRenderer) this.renderer = null + // For serving static/ files to / + this.serveStatic = pify(serveStatic(resolve(this.dir, 'static'))) // For serving .nuxt/dist/ files - this.serveStatic = pify(serveStatic(resolve(this.dir, '.nuxt', 'dist'))) + this._nuxtRegexp = /^\/_nuxt\// + this.serveStaticNuxt = pify(serveStatic(resolve(this.dir, '.nuxt', 'dist'))) // Add this.build this.build = build.bind(this) // Launch build and set this.renderer @@ -74,25 +77,27 @@ class Nuxt { // Call webpack middlewares only in development yield self.webpackDevMiddleware(req, res) yield self.webpackHotMiddleware(req, res) - return } - if (req.url.includes('/_nuxt/')) { + // Serve static/ files + yield self.serveStatic(req, res) + // Serve .nuxt/dist/ files (only for production) + if (!self.dev && self._nuxtRegexp.test(req.url)) { const url = req.url - req.url = req.url.replace('/_nuxt/', '/') - yield self.serveStatic(req, res) + req.url = req.url.replace(self._nuxtRegexp, '/') + yield self.serveStaticNuxt(req, res) req.url = url } }) .then(() => { - if (this.dev && req.url.includes('/_nuxt/') && req.url.includes('.hot-update.json')) { + if (this.dev && this._nuxtRegexp.test(req.url) && req.url.includes('.hot-update.json')) { res.statusCode = 404 return res.end() } return this.renderRoute(req.url, context) }) - .then((html) => { - if (context.nuxt.error && context.nuxt.error.statusCode) { - res.statusCode = context.nuxt.error.statusCode + .then(({ html, error }) => { + if (error) { + res.statusCode = context.nuxt.error.statusCode || 500 } res.end(html, 'utf8') }) @@ -110,13 +115,13 @@ class Nuxt { // Call rendertoSting from the bundleRenderer and generate the HTML (will update the context as well) const self = this return co(function * () { - const html = yield self.renderToString(context) + const app = yield self.renderToString(context) if (context.nuxt && context.nuxt.error instanceof Error) { context.nuxt.error = { statusCode: 500, message: context.nuxt.error.message } } - const app = self.appTemplate({ + const html = self.appTemplate({ dev: self.dev, // Use to add the extracted CSS in production - APP: html, + APP: app, context: context, files: { css: join('/_nuxt/', self.options.build.filenames.css), @@ -124,7 +129,7 @@ class Nuxt { app: join('/_nuxt/', self.options.build.filenames.app) } }) - return app + return { html, error: context.nuxt.error } }) } diff --git a/lib/server.js b/lib/server.js index af58bd2fed..e75a24aaf0 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,41 +1,14 @@ 'use strict' const http = require('http') -const co = require('co') -const pify = require('pify') -const serveStatic = require('serve-static') -const { resolve } = require('path') class Server { constructor (nuxt) { - this.server = http.createServer(this.handle.bind(this)) - this.serveStatic = pify(serveStatic(resolve(nuxt.dir, 'static'))) - this.nuxt = nuxt + this.server = http.createServer(nuxt.render.bind(nuxt)) return this } - handle (req, res) { - const method = req.method.toUpperCase() - const self = this - - if (method !== 'GET' && method !== 'HEAD') { - return this.nuxt.render(req, res) - } - co(function * () { - if (req.url.includes('/static/')) { - const url = req.url - req.url = req.url.replace('/static/', '/') - yield self.serveStatic(req, res) - req.url = url - } - }) - .then(() => { - // File not found - this.nuxt.render(req, res) - }) - } - listen (port, host) { host = host || 'localhost' port = port || 3000 diff --git a/package.json b/package.json index fa4e6643d0..620f1d0fd9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuxt", - "version": "0.2.6", + "version": "0.3.0", "description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)", "main": "index.js", "license": "MIT",