浏览器保持“等待本地主机”

时间:2018-12-10 14:08:14

标签: node.js express time localhost out

在我的index.js文件中:

const express = require('express');
const router = express.Router();

// Do work here
router.get('/', (req, res) => {
  res.send('Hey! It works!');
});

module.exports = router;

我能够提出第一个请求,但随后的每个请求仅等待localhost超时。

app.js看起来像:

const express = require('express');
const session = require('express-session');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')(session);
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const passport = require('passport');
const promisify = require('es6-promisify');
const flash = require('connect-flash');
const expressValidator = require('express-validator');
const routes = require('./routes/index');
const helpers = require('./helpers');
const errorHandlers = require('./handlers/errorHandlers');

// create our Express app
const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views')); // this is the folder where we keep our pug files
app.set('view engine', 'pug'); // we use the engine pug, mustache or EJS work great too

// serves up static files from the public folder. Anything in public/ will just be served up as the file it is
app.use(express.static(path.join(__dirname, 'public')));

// done! we export it so we can start the site in start.js
module.exports = app;

start.js文件如下所示。我应该补充一点,这不是我自己的代码-它是Wes Bos的node.js教程的一部分。

const mongoose = require('mongoose');

// import environmental variables from our variables.env file
require('dotenv').config({ path: 'variables.env' });

// Connect to our Database and handle any bad connections
mongoose.connect(process.env.DATABASE);
mongoose.Promise = global.Promise; // Tell Mongoose to use ES6 promises
mongoose.connection.on('error', (err) => {
  console.error(`        → ${err.message}`);
});

// READY?! Let's go!


// Start our app!
const app = require('./app');
app.set('port', process.env.PORT || 7777);
const server = app.listen(app.get('port'), () => {
  console.log(`Express running → PORT ${server.address().port}`);
});

1 个答案:

答案 0 :(得分:0)

设法解决了我自己的问题,所以我向所有在Wes Bos Node.js教程中遇到相同问题的人进行报告。

解决方案是仔细检查我的variables.env文件-我在用户名和密码字段的左边有个尖括号。如果遇到此问题,请确保您的DATABASE环境变量看起来像这样:

DATABASE=mongodb://dang:dang@ds057833.mlab.com:57833/dang-thats-delicious