【问题标题】:static resource not loaded in node app.js file节点 app.js 文件中未加载静态资源
【发布时间】:2016-01-08 05:55:45
【问题描述】:

我的 app.js 如下

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');                         //add for Mongo support
mongoose.connect('mongodb://localhost/flapperNews');        //connect to Mongo

var index = require('./routes/index');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

//rout to views
app.use('/', index);

app.get('/', function (req, res) {
    res.send('Application Started');
    console.log("Application Started.")
});
app.listen(3000);

module.exports = app;

我的目录如下 root -> node_modules -> 所有资源 根 -> 公共 -> javascripts -> angularApp.js 我收到以下错误

【问题讨论】:

    标签: angularjs node.js mean ejs


    【解决方案1】:

    我认为,您的文件 angularApp.js 可在此网址中找到 http://localhost:3000/javascripts/angularApp.js

    【讨论】:

      【解决方案2】:

      nodejs 通常会阻止公用文件夹(根目录)之外的所有内容,以供客户端查看

      app.use(express.static(path.join(__dirname, 'public')));
      

      这会将公用文件夹映射到:/public

      因此客户端无法访问您系统上的任何其他目录

      您需要更改 app.use 或文件夹;)

      【讨论】:

        猜你喜欢
        • 2019-01-31
        • 1970-01-01
        • 2017-10-13
        • 2015-01-05
        • 2016-12-30
        • 2020-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多