2013-08-28 77 views
3

使用http://apidocjs.com/即可創建我正在構建的Express.js API的公共文檔。我的問題是,如何使用Express.js路由和提供文檔?如何在Express.js上託管jsdoc/apidoc

這是我的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'));; 

下面是我用它來創建文檔,我的呼嚕聲文件:

'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( '..對於每一頁?一個教程在某處會很棒

在此先感謝

回答

0

我的問題竟然是,我是在另一個文件中使用__dirname期待它是根目錄。現在我明白這一點是顯而易見的。如果一個文件包含__dirname,那麼__dirname = [the directory of that file]不是該文件(模塊)所需的目錄。

這是造成查找錯誤。

感謝您的幫助!

7

您不需要聲明爲每個靜態文件提供服務的路由。

這應該是足夠了:

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')); 
+0

如果您使用某種類似auth的類似jwt,'app.use(..'行應該在使用JWT/auth中間件之前 – avck