1

我试过 localhost:3000/index.html (我放静态页面的地方), localhost:3000/javascripts/dio.js (我放 js 的地方) localhost:3000/images/dio1.jpg (我放图片的地方),全部无法访问。为什么?怎么了?

我的 app.js 定义:

var express = require('express')
var app=express()
app.bcrypt = require('bcrypt')
app.im=require('imagemagick')
app.fs=require('fs')
app.application_root = __dirname
app.path = require("path")
app.mongoose = require("mongoose")

var config=require('./config.js')(app,express)

var models = require('./models/models.js')(app.mongoose)
models.UserModel = models.UserModel
models.CommentModel = models.CommentModel
models.ItemModel = models.ItemModel

require('./routes')(app, models)

app.listen(3000);
console.log("Listening on port 3000")

这是我的 config.js 定义:

module.exports = function(app, express, mongoose){
    var config=this

    app.configure(function (){
        app.set('views',__dirname+'/views')
        app.set('view engine','jade')
        app.set('view engine', {layout:true})

        app.use(express.bodyParser())
        app.use(express.methodOverride())
        app.use(express.cookieParser())
        app.use(express.session({secret: 'topsecret',store: new express.session.MemoryStore}))
        app.use(app.router)
        app.use(express.static(app.path.join(app.application_root+"public")))
        app.use(express.errorHandler({dumpExceptions:true,showStack:true}))
        app.use(express.bodyParser({keepExtensions: true, uploadDir:"./public/uploads"}))
    })

    /*DB part:*/
    app.mongoose.connect('mongodb://localhost/dio_database')

    return config
}
4

2 回答 2

2

试试这种风格:

app.use('/public', express.static(__dirname + "/public"));
于 2012-12-05T22:53:52.037 回答
1

而不是连接加入:

 app.path.join(app.application_root+"public")

每个部分都应作为单独的参数传递给path.join

 app.path.join(app.application_root, "public")

或者,如果您希望连接,__dirname则 不会有尾部斜杠application_root,因此您需要添加一个:

 app.application_root + '/public'
于 2012-12-05T22:49:32.423 回答