@nuxt/cli

  1. // dist/cli.js
  2. async function run (_argv) { /* ... */ }
  3. // dist/cli-command.js
  4. async function _import (modulePath) { /* ... */ }
  5. const builder = () => _import('@nuxt/builder');
  6. const webpack = () => _import('@nuxt/webpack');
  7. const generator = () => _import('@nuxt/generator');
  8. const core = () => _import('@nuxt/core');
  9. const importModule = _import;

@nuxt/builder

  1. class Builder {
  2. getBundleBuilder (BundleBuilder) {
  3. if (typeof BundleBuilder === 'object') {
  4. return BundleBuilder
  5. }
  6. const context = new BuildContext(this);
  7. if (typeof BundleBuilder !== 'function') {
  8. ({ BundleBuilder } = require('@nuxt/webpack'));
  9. }
  10. return new BundleBuilder(context)
  11. }
  12. }
  13. exports.Builder = Builder;

@nuxt/config

  1. // require("@nuxt/config")
  2. // @nuxt/config
  3. const defaultNuxtConfigFile = 'nuxt.config';
  4. exports.defaultNuxtConfigFile = defaultNuxtConfigFile;
  5. exports.getDefaultNuxtConfig = getDefaultNuxtConfig;
  6. exports.getNuxtConfig = getNuxtConfig;

@Nuxt/cli

  1. // dist/cli-dev.js
  2. const dev = {
  3. //...
  4. async _startDev (cmd, argv) {
  5. // @nuxt/core new Nuxt(...args)
  6. const nuxt = await cmd.getNuxt(config);
  7. // Wait for nuxt to be ready
  8. await nuxt.ready();
  9. // Start listening
  10. await nuxt.server.listen();
  11. }
  12. //...
  13. }

@nuxt/core

  1. const server = require('@nuxt/server');
  2. class Nuxt extends Hookable {
  3. ready () {
  4. if (!this._ready) {
  5. this._ready = this._init();
  6. }
  7. return this._ready
  8. }
  9. _initServer () {
  10. if (this.server) {
  11. return
  12. }
  13. this.server = new server.Server(this);
  14. this.renderer = this.server;
  15. this.render = this.server.app;
  16. utils.defineAlias(this, this.server, ['renderRoute', 'renderAndGetWindow', 'listen']);
  17. }
  18. }

@nuxt/service

  1. const connect = _interopDefault(require('connect'));
  2. const http = _interopDefault(require('http'));
  3. const https = _interopDefault(require('https'));
  4. class Listener {
  5. async listen () {
  6. // this.app = connect();
  7. // Initialize underlying http(s) server
  8. const protocol = this.https ? https : http;
  9. const protocolOpts = typeof this.https === 'object' ? [this.https] : [];
  10. // opts = [app] , app = function(req, res, next) {...}
  11. const opts = protocolOpts.concat(this.app);
  12. // 在这里创建服务了
  13. this._server = protocol.createServer.apply(protocol, opts);
  14. // Call server.listen
  15. // Prepare listenArgs
  16. const listenArgs = this.socket ? { path: this.socket } : { host: this.host, port: this.port };
  17. listenArgs.exclusive = false;
  18. // Call server.listen
  19. try {
  20. this.server = await new Promise((resolve, reject) => {
  21. this._server.on('error', error => reject(error));
  22. // 在此次打开web service 监听
  23. const s = this._server.listen(listenArgs, error => error ? reject(error) : resolve(s));
  24. });
  25. } catch (error) {
  26. return this.serverErrorHandler(error)
  27. }
  28. }
  29. }
  30. class Server {
  31. constructor (nuxt) {
  32. // Create new connect instance
  33. this.app = connect();
  34. }
  35. async listen (port, host, socket) {
  36. const listener = new Listener({...})
  37. return listener;
  38. }
  39. }
  40. exports.Listener = Listener;
  41. exports.Server = Server;

connect

Connect 是一个可扩展的 HTTP 服务器框架,用于使用称为中间件的“plugins”的节点。

  1. var EventEmitter = require('events').EventEmitter;
  2. var http = require('http');
  3. var merge = require('utils-merge');
  4. function createServer() {
  5. function app(req, res, next){ app.handle(req, res, next); }
  6. merge(app, proto);
  7. merge(app, EventEmitter.prototype);
  8. app.route = '/';
  9. app.stack = [];
  10. return app;
  11. }
  12. proto.listen = function listen() {
  13. var server = http.createServer(this);
  14. return server.listen.apply(server, arguments);
  15. };
  16. module.exports = createServer;

Connect 的使用

  1. var connect = require('connect');
  2. var http = require('http');
  3. var app = connect();
  4. // gzip/deflate outgoing responses
  5. var compression = require('compression');
  6. app.use(compression());
  7. // store session state in browser cookie
  8. var cookieSession = require('cookie-session');
  9. app.use(cookieSession({
  10. keys: ['secret1', 'secret2']
  11. }));
  12. // parse urlencoded request bodies into req.body
  13. var bodyParser = require('body-parser');
  14. app.use(bodyParser.urlencoded({extended: false}));
  15. // respond to all requests
  16. app.use(function(req, res){
  17. res.end('Hello from Connect!\n');
  18. });
  19. //create node.js http server and listen on port
  20. http.createServer(app).listen(3000);