节点.js显示在本地主机上,但不在同一台机器上的http上显示

Node.js showing on localhost but not on http on same machine

本文关键字:显示 机器 http 一台 js 主机 节点      更新时间:2023-09-26

我做了一个简单的node.js服务器,它显示index.html。当我在本地主机上运行它时,它会显示页面,但是当我尝试在 http:ip:8000 上运行它时,它说"此网页不可用"。

这是我的代码:

    // server2.js (Express 4.0)
var express        = require('express');
var morgan         = require('morgan');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
var app            = express();
app.use(express.static(__dirname + '/public'));     // set the static files location /public/img will be /img for users
app.use(morgan('dev'));                     // log every request to the console
app.use(bodyParser());                      // pull information from html in POST
app.use(methodOverride());                  // simulate DELETE and PUT

var router = express.Router();
var notes = [
  {username: 'Mohit1406', password: 'midkm' ,firstName: 'mxkz', lastName: 'ckzmk', dob: '1994-05-08', gender: 'male'},
];
router.get('/note', function(req, res) {
  res.send(notes);
});
router.post('/note', function(req, res) {
  var note = req.body;
  notes.push(note);
  res.send(note);
});
app.use('/api', router);

app.listen(8000,'0.0.0.0');
console.log('Open http://localhost:8000 to access the files now');          // shoutout to the user
app.listen(8000);

主机名是可选的

server.listen(port, [hostname], [backlog], [callback])#

开始接受指定端口和主机名上的连接。如果 省略主机名,服务器将接受定向到的连接 任何 IPv4 地址 (INADDR_ANY)。