对nodejs非常陌生,尝试在linux服务器上构建一个非常简单的服务器。由于某种原因,当我访问该网址时,我会看到文件夹中的文件。我正在学习,所以我为每个学习练习创建了一个文件夹。
这是我的代码: 服务器.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 服务器
,我收到在 ports3000 上运行的服务器
消息,但在浏览器中我看到这个:
任何帮助都会很棒,
最佳答案
您正在访问类似的网址
http://localhost/yourprojectfolder
这就是它显示文件夹索引的原因。如果是本地项目,则访问
http://localhost:3000 //3000 your port number
否则访问您的服务器的 URL
http://example.com:3000
关于node.js 查看文件夹中的文件而不是应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44254089/