【问题标题】:Node Server Does not Load HTML Page节点服务器不加载 HTML 页面
【发布时间】:2016-10-17 19:14:57
【问题描述】:

我的代码有问题... 我在 NodeJS 上有一个服务器,它编译得很好,没有错误。 但是当我尝试在同一个根目录下编译 HTML 文件时,localhost 返回 page not found。

请检查我下面的代码和文件树。纠正我的错误。

[SERVER.JS]

var express     = require('express');
var app         = express();
var bodyParser  = require('body-parser');
var morgan      = require('morgan');
var mongoose    = require('mongoose');
var path = require("path");
var passport    = require('passport');
var config      = require('./config/database'); // get db config file
var User        = require('./app/models/user'); // get the mongoose model
var port        = process.env.PORT || 3000;
dbName          = 'my_db_name';
dbHost          = 'localhost';
var jwt         = require('jwt-simple');

// get our request parameters
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// log to console
app.use(morgan('combined'));

// Use the passport package in our application
app.use(passport.initialize());

// demo Route (GET http://localhost:3000)
app.get('/', function(req, res) {
  res.send('Hello! The Application is running @ http://localhost:' + port + '/api');
});

// connect to database
//mongoose.connect(config.database);
mongoose.connect('mongodb://'+dbHost+'/'+dbName);//try this if the the first does not work

// serve client side code.
app.use('/',express.static('/'));

// pass passport for configuration
require('./config/passport')(passport);

// bundle our routes
var apiRoutes = express.Router();


// connect the api routes under /api/*
app.use('/api', apiRoutes);

// Start the server
app.listen(port);
console.log('Server running @: http://localhost:' + port);

[HOME.HTML]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Easytime Page</title>


<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="css/w3.css" />

</head>

<body ng-app="starter" ng-controller="AppCtrl">
  <ui-view></ui-view>  


<div class="w3-container w3-border" style="height:auto;" ng-app = "loginForm">
    <!--- header --->
    <div class="w3-container w3-teal">
        <header>
            <h2>Easytime Home</h2>
        </header>
    </div>

    <div ng-view></div>
    <div class="myContainer w3-left w3-content">
        <h3 style="text-align:center">Welcome</h3>       
    </div>

    <!--footer -->
    <footer>
        <div class="w3-container w3-teal others" style="margin-top:70px;"><h3>Copyright &copy; 2016, Eyo Eyo. All Rights Reserved!</h3></div>
    </footer>
</div>
</body>
</html>

<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/services.js"></script>
<script src="js/constants.js"></script>

FILE TREE ARRANGEMENT

【问题讨论】:

    标签: html node.js server localhost


    【解决方案1】:

    我通过逐行执行每一行代码解决了这个问题。我试图将整个代码与另一个服务器示例进行比较。我注意到我没有包含 app.use('/client',express.static('client')); 以提供客户端文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-09
      • 2021-02-13
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 2020-11-08
      • 1970-01-01
      相关资源
      最近更新 更多