我已通过以下步骤在Ubuntu 16.04操作系统上安装了Swagger Editor:
1. sudo apt-get install nodejs-legacy
2. git clone https://github.com/swagger-api/swagger-editor.git
3. sudo apt-get install npm
4. cd swagger-editor
5. npm start
6. I edited the APIs I wanted using Swagger Editor
7. I generated a NodeJS Server from Swagger Editor Menu
8. I extracted my server zip file and CD into it.
9. npm install connect
10. npm install swagger-tools
11. npm install js-yaml
12. node index.js <-- START MY SERVER
结果是:
me@ubuntu:~/Downloads/nodejs-server-server$ node index.js
Your server is listening on port 8081 (http://localhost:8081)
Swagger-ui is available on http://localhost:8081/docs
问题: 您在这里看到它仅在 localhost 上监听。
如何使服务器侦听服务器的实际IP地址而不是本地主机的IP地址?我想从另一台PC访问它吗?
这是Swagger生成的默认 index.js 文件:
'use strict';
var fs = require('fs'),
path = require('path'),
http = require('http');
var app = require('connect')();
var swaggerTools = require('swagger-tools');
var jsyaml = require('js-yaml');
var serverPort = 8080;
// swaggerRouter configuration
var options = {
swaggerUi: path.join(__dirname, '/swagger.json'),
controllers: path.join(__dirname, './controllers'),
useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
};
// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var spec = fs.readFileSync(path.join(__dirname,'api/swagger.yaml'), 'utf8');
var swaggerDoc = jsyaml.safeLoad(spec);
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// Validate Swagger requests
app.use(middleware.swaggerValidator());
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());
// Start the server
http.createServer(app).listen(serverPort, function ()
{
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
});
});