链接到另一个HTML文件在本地主机上工作,但不是当我上传到服务器上

我的文件夹结构如下所示: 更新

/public twitter.html /styles styles.css /views index.html server.js package.json 

我有一个链接到twitter.html的index.html。 我在.js文件中使用Express和Node.js,所以我这样链接:

 <a href="/twitter">Twitter</a> 

我不能使用<a href="twitter.html">Twitter</a>因为它不适用于Express,它只会给我这个: https : <a href="twitter.html">Twitter</a> (在networking浏览器上)当它应该是这样的: https : //imgur.com/a/lyHja (在本地主机上)

当我运行本地主机,一切都按预期工作,我点击链接到Twitter,它的工作原理。 我从http://localhost:3000/转到http://localhost:3000/twitter

但是,当我在我的networking浏览器上打开index.html,然后点击链接到Twitter,它说:“你的文件没有find”。 我从file:///Users/name/twitter/views/index.htmlfile:///twitter

当我将这些file upload到networking服务器,然后点击twitter链接时,它显示“找不到请求的URL / twitter在这台服务器上找不到。”。 我从http://mypage.com/myusername/views/index.htmlhttp://mypage.com/twitter

任何想法我做错了,因为在本地主机上运行时,一切正常?

我的script.js看起来像这样: 更新

 const express = require('express') const bodyParser = require('body-parser') const MongoClient = require('mongodb').MongoClient const path = require('path') const app = express() let db MongoClient.connect('mongodb://someuser:somepassword@cluster0-shard-00-00-fquoc.mongodb.net:27017,cluster0-shard-00-01-fquoc.mongodb.net:27017,cluster0-shard-00-02-fquoc.mongodb.net:27017/twitter?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin', (err, database) => { if (err) return console.log(err) db = database app.listen(3000, () => { console.log('listening on 3000') }) }) app.engine('html', require('ejs').renderFile) app.set('view engine', 'html') app.set('views', path.join(__dirname, 'views')); app.use(bodyParser.urlencoded({extended: true})) //app.use(express.static(path.join(__dirname, 'public'))); //app.use('/public', express.static(path.join(__dirname, 'public'))) app.get('/public/twitter', (req, res) => { db.collection('tweets').find().toArray((err, result) => { if (err) return console.log(err) res.send('/public/twitter.html') }) }) app.get('/', (req, res) => { return res.render('index'); }); app.post('/tweets', (req, res) => { db.collection('tweets').save(req.body, (err, result) => { if (err) return console.log(err) console.log('saved to database') res.redirect('/twitter') }) }) 

如果你的twitter页面在/ views文件夹中,那么就相应地访问它。 <a href="/views/twitter.html> Twitter </a>

更新 :现在你张贴你的路由代码,这是你的问题。 app.use(express.static(path.join(__dirname, 'public'))); https://expressjs.com/en/starter/static-files.html

 app.use('/public', express.static(path.join(__dirname, 'public'))) 

摆脱你的意见,并把twitter页面粘在公共目录中,然后做/ public / twitter,你会没事的

 app.get('/public/twitter', (req, res) => { db.collection('tweets').find().toArray((err, result) => { if (err) return console.log(err) res.sendFile( __dirname + "/public/" + "twitter.html" ); }) })