@nuxt/cli
// dist/cli.js
async function run (_argv) { /* ... */ }
// dist/cli-command.js
async function _import (modulePath) { /* ... */ }
const builder = () => _import('@nuxt/builder');
const webpack = () => _import('@nuxt/webpack');
const generator = () => _import('@nuxt/generator');
const core = () => _import('@nuxt/core');
const importModule = _import;
@nuxt/builder
class Builder {
getBundleBuilder (BundleBuilder) {
if (typeof BundleBuilder === 'object') {
return BundleBuilder
}
const context = new BuildContext(this);
if (typeof BundleBuilder !== 'function') {
({ BundleBuilder } = require('@nuxt/webpack'));
}
return new BundleBuilder(context)
}
}
exports.Builder = Builder;
@nuxt/config
// require("@nuxt/config")
// @nuxt/config
const defaultNuxtConfigFile = 'nuxt.config';
exports.defaultNuxtConfigFile = defaultNuxtConfigFile;
exports.getDefaultNuxtConfig = getDefaultNuxtConfig;
exports.getNuxtConfig = getNuxtConfig;
@Nuxt/cli
// dist/cli-dev.js
const dev = {
//...
async _startDev (cmd, argv) {
// @nuxt/core new Nuxt(...args)
const nuxt = await cmd.getNuxt(config);
// Wait for nuxt to be ready
await nuxt.ready();
// Start listening
await nuxt.server.listen();
}
//...
}
@nuxt/core
const server = require('@nuxt/server');
class Nuxt extends Hookable {
ready () {
if (!this._ready) {
this._ready = this._init();
}
return this._ready
}
_initServer () {
if (this.server) {
return
}
this.server = new server.Server(this);
this.renderer = this.server;
this.render = this.server.app;
utils.defineAlias(this, this.server, ['renderRoute', 'renderAndGetWindow', 'listen']);
}
}
@nuxt/service
const connect = _interopDefault(require('connect'));
const http = _interopDefault(require('http'));
const https = _interopDefault(require('https'));
class Listener {
async listen () {
// this.app = connect();
// Initialize underlying http(s) server
const protocol = this.https ? https : http;
const protocolOpts = typeof this.https === 'object' ? [this.https] : [];
// opts = [app] , app = function(req, res, next) {...}
const opts = protocolOpts.concat(this.app);
// 在这里创建服务了
this._server = protocol.createServer.apply(protocol, opts);
// Call server.listen
// Prepare listenArgs
const listenArgs = this.socket ? { path: this.socket } : { host: this.host, port: this.port };
listenArgs.exclusive = false;
// Call server.listen
try {
this.server = await new Promise((resolve, reject) => {
this._server.on('error', error => reject(error));
// 在此次打开web service 监听
const s = this._server.listen(listenArgs, error => error ? reject(error) : resolve(s));
});
} catch (error) {
return this.serverErrorHandler(error)
}
}
}
class Server {
constructor (nuxt) {
// Create new connect instance
this.app = connect();
}
async listen (port, host, socket) {
const listener = new Listener({...})
return listener;
}
}
exports.Listener = Listener;
exports.Server = Server;
connect
Connect 是一个可扩展的 HTTP 服务器框架,用于使用称为中间件的“plugins”的节点。
var EventEmitter = require('events').EventEmitter;
var http = require('http');
var merge = require('utils-merge');
function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
}
proto.listen = function listen() {
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
module.exports = createServer;
Connect 的使用
var connect = require('connect');
var http = require('http');
var app = connect();
// gzip/deflate outgoing responses
var compression = require('compression');
app.use(compression());
// store session state in browser cookie
var cookieSession = require('cookie-session');
app.use(cookieSession({
keys: ['secret1', 'secret2']
}));
// parse urlencoded request bodies into req.body
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
// respond to all requests
app.use(function(req, res){
res.end('Hello from Connect!\n');
});
//create node.js http server and listen on port
http.createServer(app).listen(3000);