我的代码中有问题... 我在NodeJS上有一个服务器,它可以很好地编译而不会出错。 但是当我尝试在同一个根目录下编译HTML文件时,localhost会返回找不到的页面。
请检查下面的代码和文件树。在我错的地方纠正我。
[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 © 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>
答案 0 :(得分:0)
我通过一个接一个地遵循每行代码解决了这个问题。我试图将整个代码与另一个服务器示例进行比较。我注意到我没有包含app.use('/client',express.static('client'));
来提供客户端文件。