5

我使用http://apidocjs.com/为我正在构建的 Express.js API 创建公共文档。我的问题是,如何使用 Express.js 来路由和提供文档?

这是我的 Express 服务器设置:

/** Load config into globally defined __config
 * @requires fs */
var fs = require('fs');
__config = JSON.parse(fs.readFileSync('config/config.json'));

/** Custom Logging Moduele
* @requires ninja_modules/jacked-logger */
log = require('./ninja_modules/jacked-logger');

/** Configure the Express Server
 * @requires express
 * @param {function} the callback that configures the server */
var express = require('express');
var app = express();
app.configure(function() {
    /** Sets default public directory */
    app.use(express.static(__dirname + '/public'));
    /** Sets root views directory */
    app.set('views', __dirname + '/public/views');
    /** Compress response data with gzip / deflate. */
    app.use(express.compress());
    /** Request body parsing middleware supporting JSON, urlencoded, and multipart requests. */
    app.use(express.bodyParser());
    /** Compress response data with gzip / deflate. */
    app.use(express.methodOverride());
    /** Set Express as the Router */
    app.use(app.router);
    /** .html files, EJS = Embedded JavaScript */
    app.engine('html', require('ejs').renderFile);
    /** Default view engine name for views rendered without extensions */
    app.set('view engine', 'html');

    /** Custom Error Logging
     * @requires ninja_modules/jacked-logger
     * @param {object} err - error object
     * @param {object} req - reqiuest object
     * @param {object} res - response object
     * @param {function} next - go to the next error */
    app.use(function(err, req, res, next) {
        log.error(err.stack);
        res.status(500);
        next(err);
    });
});
/** Set express to listen to the port defined in the configuration file */
var appServer = app.listen(__config.port, function(){
    log.sys("Express server listening on port " + appServer.address().port + " in " + app.settings.env + " mode");
});

// add documentation
app.use('/api', express.static(__dirname + '/public/documentation/api'));
app.use('/dev', express.static(__dirname + '/public/documentation/developer'));;

这是我用来创建文档的 grunt 文件:

'use strict';

module.exports = function(grunt) {
    grunt.initConfig({
        jsdoc : {
            dist : {
                src: ['*.js', 'config/*.json', 'ninja_modules/*.js','workers/*.js'], 
                options: {
                    destination: 'public/documentation/developer',
                    private: true
                }
            }
        },
        apidoc: {
            ninjapi: {
                src: 'router/',
                dest: 'public/documentation/api/',
                options: {
                    includeFilters: [ ".*\\.js$" ]
                }            
            }
        }
    });
    grunt.loadNpmTasks('grunt-jsdoc');
    grunt.loadNpmTasks('grunt-apidoc');
    grunt.registerTask('default', ['jsdoc','apidoc']);
}

有谁知道我如何在不为每个页面声明 app.get('.. 的情况下托管我的文档?某个地方的教程会很棒。

提前致谢。

4

2 回答 2

8

您无需声明为每个静态文件提供服务的路由。

这应该足够了:

app.use('/api', express.static(__dirname + '/public/documentation/api'));

但是如果public/documentation/api目录不包含索引文件,你会得到一个请求错误。

所以改为这样做,它允许您浏览目录:

app.use('/api', express.static(__dirname + '/public/documentation/api'));
app.use('/api', express.directory(__dirname + '/public/documentation/api'));
于 2013-08-28T15:20:03.540 回答
0

My issue turned out to be that I was using __dirname in another file expecting it to be the root directory. This is obvious now that I think about it. If a file contains __dirname then __dirname = [the directory of that file] not the directory that the file (module) was required into.

This was causing look up errors.

Thanks for the help!

于 2013-08-28T17:22:30.487 回答