对nodejs来说很新,试图在linux服务器上构建一个非常简单的服务器。出于某种原因,当我去网址时,我看到文件夹中的文件。我正在学习所以我为每个学习练习创建了一个文件夹。
这是我的代码: server.js
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var port = 3000;
var app = express();
app.use(function(req, res, next){
console.log('Time: ', Date.now());
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
res.send('Hello World!');
});
app.get('/about', function(req, res){
res.send('About Page');
});
app.listen(port);
console.log('Server started on ports '+port);
module.exports = app;
这是package.json
{
"name": "myexpress",
"version": "1.0.0",
"description": "Simple express application",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "*",
"body-parser":"*"
}
}
我在文件夹项目中运行node server
,我收到了Server running on ports3000
按摩,但在浏览器中我看到了:
任何帮助都可能很棒,
答案 0 :(得分:4)
您正在访问类似
的网址http://localhost/yourprojectfolder
这就是它显示文件夹索引的原因。如果是本地项目,请访问
http://localhost:3000 //3000 your port number
否则请访问您服务器的网址
http://example.com:3000