Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 链接到另一个html文件在本地主机上工作,但在服务器上上载时不会_Javascript_Html_Node.js_Express_Twitter - Fatal编程技术网

Javascript 链接到另一个html文件在本地主机上工作,但在服务器上上载时不会

Javascript 链接到另一个html文件在本地主机上工作,但在服务器上上载时不会,javascript,html,node.js,express,twitter,Javascript,Html,Node.js,Express,Twitter,我的文件夹结构如下:已更新 /public twitter.html /styles styles.css /views index.html server.js package.json const express = require('express') const bodyParser = require('body-parser') const MongoClient = require('mongodb').MongoClient const path = re

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

/public
  twitter.html
  /styles
    styles.css
/views
  index.html
server.js
package.json 
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')
  })
})
我有一个index.html链接到twitter.html。我在.js文件中使用Express和Node.js,因此我链接如下:

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

如果您的twitter页面位于/views文件夹中,请相应地访问它。

更新:现在您发布了您的路由代码,这是您的问题<代码>应用程序使用(express.static(path.join(uu 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" );
  })
})

.html扩展名不适用于Express。我一开始就是这么做的,但在使用Express/Node.js之后,我不得不为它制作路由器。按照你说的做,我可以访问该网站,但它看起来是这样的:imgur.com/a/DL9aM,在localhost上它说:Cannot GET/twitter.html我已经更新了我的帖子,以显示我的.js文件在路由方面的外观。谢谢你的解释。我删除了
app.use(expr..
代码行,并在index.html中编写了
,在本地主机上运行。我还尝试在我的web浏览器上通过index.html单击twitter.html,但它仍然显示无法获取文件。我误解了什么吗?仍然不明白为什么它在浏览器上无法运行。编辑了我的答案。当我删除时,它与CSS一起工作
app.use(expr..
,但不要在web浏览器中打开index.html然后单击twitter.html链接。好的,我已经更新了原始帖子中的.js文件和文件夹结构。在index.html中,我像这样链接
。在浏览器上,我现在看到一个只有“twitter.html”和localhost的空白页面“找不到您的文件".我忘记添加或删除任何内容了吗?-php与Express没有什么不同吗?我在.js文件中使用了jQuery,但不得不更改为node和Express,以使其与数据库一起工作,但谢谢,也许我应该将此项目搁置!对服务器端语言一点也不好。再次感谢你!是的,我是个白痴,你只是在发送string,抱歉,这是漫长的一天,您想发送文件.res.sendFile(uu dirname+“/public/”+“twitter.html”);尝试几种组合,您可以尝试使用和不使用u dirname
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" );
  })
})