Javascript 这就是为什么routes.js中的全局规则为静态文件提供服务

Javascript 这就是为什么routes.js中的全局规则为静态文件提供服务,javascript,node.js,angularjs,express,mean-stack,Javascript,Node.js,Angularjs,Express,Mean Stack,为了修复此更改,请将\uuu dirname+'/public'更改为./../app/public,或者更好地将服务器.js文件放在应该的位置并更新路径。 我还可以看到,您在routes.js中使用的是index.html的绝对完整路径,而不是相对路径,因此您的应用程序似乎需要整理。我无法确认,但我感觉您的应用程序中的路径是错误的 express设置中的关键部分包括: app.use(express.static(uu dirname+/public)) 及 app.get('*',函数(re

为了修复此更改,请将
\uuu dirname+'/public'
更改为
./../app/public
,或者更好地将
服务器.js
文件放在应该的位置并更新路径。

我还可以看到,您在
routes.js
中使用的是
index.html
的绝对完整路径,而不是相对路径,因此您的应用程序似乎需要整理。

我无法确认,但我感觉您的应用程序中的路径是错误的

express设置中的关键部分包括:

app.use(express.static(uu dirname+/public))

app.get('*',函数(req,res){
res.sendfile('/Users/../app/public/index.html');

第一条规则捕获并返回
\uu dirname+'/public'
中的任何静态文件
第二个返回
index.html
,用于其他任何内容

问题是您的
server.js
不在apps目录中(我可以看到这一点,因为您使用
。/../app/routes.js
访问
routes.js
)这意味着
\uu dirname+'/public'
没有指向公共目录。这就是为什么
routes.js
中的全局规则为静态文件提供服务的原因

为了修复此更改,请将
\uuu dirname+'/public'
更改为
./../app/public
,或者更好地将
服务器.js
文件放在应该的位置并更新路径。

我还可以看到,您在
routes.js
中使用的是
index.html
的绝对完整路径,而不是相对路径,因此您的应用程序似乎需要整理。

我无法确认,但我感觉您的应用程序中的路径是错误的

express设置中的关键部分包括:

app.use(express.static(uu dirname+'/public'));

app.get('*',函数(req,res){
res.sendfile('/Users/../app/public/index.html');

第一条规则捕获并返回
\uu dirname+'/public'
中的任何静态文件
第二个返回
index.html
,用于其他任何内容

问题是您的
server.js
不在apps目录中(我可以看到这一点,因为您使用
。/../app/routes.js
访问
routes.js
)这意味着
\uu dirname+'/public'
没有指向公共目录。这就是为什么
routes.js
中的全局规则为静态文件提供服务的原因

为了修复此更改,请将
\uuu dirname+'/public'
更改为
./../app/public
,或者更好地将
服务器.js
文件放在应该的位置并更新路径。


我还可以看到,您在
routes.js
中使用的是
index.html
的绝对完整路径,而不是相对路径,因此您的应用程序似乎需要整理。

您的server.js在发布您的routes.js后看起来很好,您的目录结构也会很有用。感谢您的关注,我已经添加了my routes.js和my dir.struct我们需要澄清的是,应用程序根本无法工作。没有加载javascript或css,因此没有angularjs和bootstrap。所有的JS和css都将作为index.html文件返回到浏览器。发布Your routes.JS后,你的server.JS看起来很好,你的目录结构也会很有用。感谢你的关注,我已经添加了我的routes.JS还有我的dir.structure要澄清的问题是,应用程序根本不工作。没有javascript或css正在加载,因此没有angularjs和bootstrap。所有的JS和css都将作为index.html文件返回到浏览器。发布Your routes.JS后,你的server.JS看起来很好,你的目录结构也会很有用。谢谢你的关注,我已经完成了添加了my routes.js和my dir.structure以澄清问题:应用程序根本无法工作。没有加载javascript或css,因此没有angularjs和bootstrap。所有js和css都将作为index.html文件返回到浏览器。发布routes.js后,你的server.js看起来很好,你的目录结构也会很有用。谢谢为了寻找,我添加了我的routes.js和我的dir.structure,以澄清问题是,应用程序根本不工作。没有javascript或css正在加载,因此没有angularjs和bootstrap。所有的js和css都将作为index.html文件返回到浏览器。这不是我想运行不同的页面,我希望索引页面工作,并且它是c目前没有。浏览器没有加载我的js/css文件,而是认为它们都包含与index.html文件相同的内容,因此浏览器会抛出语法错误,因为css/js文件以doctype开头。哦,我明白了-你需要检查server.js和public是否在同一个文件夹中。你搞定了。非常感谢。我的文件夹结构是wrong和server.js必须处于顶层,而提供的所有内容都必须在我的index.html页面的公用文件夹中。教程对建议的文件夹结构的看法非常糟糕。这并不是说我想运行不同的页面,而是我希望索引页面能够正常工作,而现在不行。bro没有加载我的js/css文件wser认为它们都包含与index.html文件相同的内容,因此浏览器会因为以doctype开头的css/js文件而引发语法错误。哦,我明白了-你需要检查server.js和public是否在同一个文件夹中。你搞定了。非常感谢。我的文件夹结构错误,server.js必须处于顶层,而一切都是错误的我的index.html页面需要在公用文件夹中接受服务。教程对建议的文件夹结构的看法非常糟糕。这并不是因为我想运行不同的页面,我希望索引页面正常工作,而它目前不工作。浏览器不加载我的js/css文件,而是认为它们都包含与index.ht相同的内容ml文件
// server.js

// modules =================================================
var express = require('express');
var app     = express();
var mongoose= require('mongoose');
var path = require('path');

// configuration ===========================================

// config files
var db = require('../config/db');

var port = process.env.PORT || 9999; // set our port
//mongoose.connect(db.url); // connect to our mongoDB database (uncomment after you enter in your own credentials in config/db.js)

app.configure(function() {
app.use(express.static(__dirname + '/public'));     // set the static files location     /public/img will be /img for users
app.use(express.logger('dev'));                     // log every request to the console
app.use(express.bodyParser());                      // have the ability to pull information from html in POST
app.use(express.methodOverride());                  // have the ability to simulate DELETE and PUT
});

// routes ==================================================
require('../../app/routes')(app); // configure our routes

// start app ===============================================
app.listen(port);                                                // startup our app at http://localhost:9999
console.log('Magic happens on port ' + port);           // shoutout to the user
exports = module.exports = app;                         // expose app
// app/routes.js

module.exports = function(app) {

// server routes ===========================================================
// handle things like api calls
// authentication routes

// sample api route
app.get('/api/nerds', function(req, res) {
    // use mongoose to get all nerds in the database
    Nerd.find(function(err, nerds) {

        // if there is an error retrieving, send the error. nothing after res.send(err) will execute
        if (err)
            res.send(err);

        res.json(nerds); // return all nerds in JSON format
    });
});

// route to handle creating (app.post)
// route to handle delete (app.delete)

// frontend routes =========================================================
// route to handle all angular requests
app.get('*', function(req, res) {
    res.sendfile('/Users/...../app/public/index.html'); // load our public/index.html file
    //res.sendfile(path, {'root': '../'});
});
app.get('*', function(req, res) {
  res.sendfile('./public/index.html'); // load our public/index.html file
});