Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.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/90.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 将/views文件夹中的index.html作为Web服务器上的登录页_Javascript_Html_Css_Node.js_Express - Fatal编程技术网

Javascript 将/views文件夹中的index.html作为Web服务器上的登录页

Javascript 将/views文件夹中的index.html作为Web服务器上的登录页,javascript,html,css,node.js,express,Javascript,Html,Css,Node.js,Express,我想将我的文件上载到一个Web服务器,在该服务器上,根文件夹中需要index.html来充当登录页。按照我现在的代码方式,我的index.html必须位于/views文件夹中。有没有办法让它登陆index.html,或者我必须将它移出并更改代码(如果有,怎么做?)我正在使用html/css、node.js和express。前2个不是很好,但“不得不”,因为我正在连接数据库。有人能帮我吗 我的文件夹结构如下所示: /public /styles styles.css /views

我想将我的文件上载到一个Web服务器,在该服务器上,根文件夹中需要index.html来充当登录页。按照我现在的代码方式,我的index.html必须位于/views文件夹中。有没有办法让它登陆index.html,或者我必须将它移出并更改代码(如果有,怎么做?)我正在使用html/css、node.js和express。前2个不是很好,但“不得不”,因为我正在连接数据库。有人能帮我吗

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

/public
  /styles
    styles.css
/views
  index.html
  twitter.html
server.js
package.json 
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.get('/twitter', (req, res) => {
  db.collection('tweets').find().toArray((err, result) => {
    if (err) return console.log(err)
    res.render('twitter', {tweets: result})
  })
})

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。如果我要将index.html移动到rootfolder,我应该像这样链接到网站吗<代码>视图/twitter

我想我们需要更多关于你提到的Web服务器的信息。如果将其配置为Express应用程序的反向代理,则不需要更改任何内容。如果它直接从文件系统提供文件,那么它应该如何与您的Express应用程序交互?什么使你认为你需要根文件夹中的
index.html
?你总是可以从根目录中的index.html重定向到视图/index.html…这很简单-peasy@skirtle我不知道是否需要index.html,但现在的文件夹结构是这样的,当我将它们上传到webfolder时,它看起来是这样的:。我对网络服务器不太了解,我的学校让每个人都有自己的网络文件夹,在那里他们可以上传文件来制作网页。在我使用数据库之前,我只是使用html/css将index.html放在根文件夹中,但现在使用数据库和Express等,我有点迷茫。如果我运行script.js,它可以在本地主机上正常工作,“登录页”是index.html。@我这样做了,但后来我遇到了另一个问题:当我单击views/index.html上的链接时,我得到了“找不到,请求的URL/twitter在此服务器上找不到”。由于Express,它们的链接如下所示。我不知道这是否与我所问的问题有关,因为它在localhost上工作得非常好@racecarjonathan只是想知道我是否可以在没有额外文件的情况下将视图作为登录页进行索引,但这基本上就是答案。谢谢