我有一个小型节点应用程序,正在对数据库进行某些操作,它在本地运行良好,但是当我尝试在云服务器中运行它时,它显示为正在运行,但无法在浏览器上看到任何内容
我正在使用express框架,并且服务器在centOS中运行。 我尝试了许多解决方案,但没有任何效果
// app.js
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
var cors = require('cors');
const http = require('http');
const routes1 = require('./routes/routes');
// initialize our express app
const app = express();
// const mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect('<My mlab details>');
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
var publicDir = require('path').join(__dirname,'/uploads/images/products');
app.use(express.static(publicDir));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cors())
app.use('/api/routes', routes1);
let port = 80;
const server = http.createServer(app);
server.listen(port)
app.listen(port, () => {
console.log('Server is up and running on port numner ' + port);
});
我希望端口能够正确侦听并存储在mlab服务器中
答案 0 :(得分:0)
从您的代码中看,您似乎正在使用快递
您在这里表示快递
const app = express();
只需将列表添加到应用
删除这两行代码
const server = http.createServer(app);
server.listen(port)
并使用这样的端口
let port = process.env.PORT || 8000;
它将解决您的问题
这是工作代码
// app.js
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
var cors = require('cors');
const http = require('http');
const routes1 = require('./routes/routes');
// initialize our express app
const app = express();
// const mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect('<My mlab details>');
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection
error:'));
var publicDir =
require('path').join(__dirname,'/uploads/images/products');
app.use(express.static(publicDir));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cors())
app.use('/api/routes', routes1);
let port = process.env.PORT || 8000;
app.listen(port, () => {
console.log('Server is up and running on port numner ' + port);
});