mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 09:27:13 +00:00
749274d22a
- Add examples/with-store/ to show how to use Vuex with nuxt.js - Fix fetch().then when fetch does not return a promise - Map static folder to /static/
70 lines
1.6 KiB
JavaScript
Executable File
70 lines
1.6 KiB
JavaScript
Executable File
#!/usr/bin/env node --harmony_proxies
|
|
|
|
const http = require('http')
|
|
const co = require('co')
|
|
const fs = require('fs')
|
|
const pify = require('pify')
|
|
const serveStatic = require('serve-static')
|
|
const Nuxt = require('../')
|
|
const { resolve } = require('path')
|
|
|
|
const rootDir = resolve(process.argv.slice(2)[0] || '.')
|
|
const nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
|
|
let options = {}
|
|
if (fs.existsSync(nuxtConfigFile)) {
|
|
options = require(nuxtConfigFile)
|
|
}
|
|
if (typeof options.rootDir !== 'string') {
|
|
options.rootDir = rootDir
|
|
}
|
|
|
|
new Nuxt(options)
|
|
.then((nuxt) => {
|
|
new Server(nuxt)
|
|
.listen(process.env.PORT, process.env.HOST)
|
|
})
|
|
.catch((err) => {
|
|
console.error(err)
|
|
process.exit()
|
|
})
|
|
|
|
class Server {
|
|
|
|
constructor (nuxt) {
|
|
this.server = http.createServer(this.handle.bind(this))
|
|
this.serveStatic = pify(serveStatic(resolve(rootDir, 'static')))
|
|
this.nuxt = 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
|
|
this.server.listen(port, host, () => {
|
|
console.log('Ready on http://%s:%s', host, port)
|
|
})
|
|
}
|
|
|
|
}
|