基本app+express+mongodb+如何不使用该端口?
这是我得到的一个基本应用程序,它使用express设置路线,并在mongo db上执行一些查询 如果我去http://localhost:8080/ 视图/hello.html中的任何内容都将显示在浏览器中。 如果我去http://localhost:8080/test “这是一个测试页面”将显示在浏览器中 我的问题是为什么我必须在地址中指定端口8080?或者换句话说,我如何在这个地址显示我想要的东西http://localhost/ 不指定端口 我知道我可以通过在这里更改8080的值来更改端口基本app+express+mongodb+如何不使用该端口?,express,port,Express,Port,这是我得到的一个基本应用程序,它使用express设置路线,并在mongo db上执行一些查询 如果我去http://localhost:8080/ 视图/hello.html中的任何内容都将显示在浏览器中。 如果我去http://localhost:8080/test “这是一个测试页面”将显示在浏览器中 我的问题是为什么我必须在地址中指定端口8080?或者换句话说,我如何在这个地址显示我想要的东西http://localhost/ 不指定端口 我知道我可以通过在这里更改8080的值来更改端口
app.listen(8080);
基本应用程序如下:
var express = require('express'),
app = express(),
cons = require('consolidate'),
crypto = require('crypto'),
MongoClient = require('mongodb').MongoClient;
app.engine('html', cons.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
MongoClient.connect('mongodb://localhost:27017/m101', function(err, db) {
if(err) throw err;
//set up a route to go to the page http://localhost:8080/ to see 'This is a test Page'
app.get('/', function(req, res){
db.collection('hw1_3').findOne(function(err, doc) {
//do stuff here
return res.render('hello', { "name" : decrypted });
});
});
//set up a route to go to the page http://localhost/test to see 'This is a test Page'
app.get('/test', function(req, res){
return res.send('This is a test Page!!', 200);
});
app.listen(8080);
console.log('Express server started on port 8080');
});
我很不懂,但我得说localhost:8080显示在www.somesite.com的位置。我不会太在意端口号。如果你把它部署到heroku或者其他你看不到的地方 http通信的默认端口是80。如果绑定到80以外的任何端口,则需要在URL中指定该端口。app.listen80将解决您的问题 在Unixy系统上,绑定到任何小于1024的端口都需要根管理员权限,因此您必须像sudo node server.js一样运行服务器才能获得端口80。在本例中,在计算机上开发时,您应该绑定到更高的端口,如8080