无法获取请求的URL,在学习Node.js和MongoDB教程时

q9rjltbz  于 2023-05-17  发布在  Go
关注(0)|答案(2)|浏览(167)

我正在按照一个指南创建一个注册表单,从here。当尝试连接http://127.0.0.1:3000/时,按照它的步骤会出现错误
无法获取请求的URL
我从C:\Program Files\MongoDB\Server\4.4\bin开始MongoDB
我唯一修改的代码是

mongoose.connect('mongodb://localhost:27017/gfg', { useNewUrlParser: true, useUnifiedTopology: true });

因为当启动应用程序时,它声明有已弃用的应用程序,但它什么也没做。
MongoDB正确启动

mongod {"t":{"$date":"2020-09-28T20:39:11.740-04:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}

和节点应用程序server listening at port 3000 connection succeeded,所以我不知道我在哪里搞砸了
我的目录看起来像这样

project root 
     ├── app.js
     ├── index.html
     └── package-lock.json
     ├── signup_success.html
     ├── style.css

Firefox给了我一个404 GET error on inspection
这里是app.js

var express=require("express"); 
var bodyParser=require("body-parser"); 
  
const mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost:27017/gfg', { useNewUrlParser: true, useUnifiedTopology: true }); 
var db=mongoose.connection; 
db.on('error', console.log.bind(console, "connection error")); 
db.once('open', function(callback){ 
    console.log("connection succeeded"); 
}) 
  
var app=express() 
  
  
app.use(bodyParser.json()); 
app.use(express.static(__dirname + "/../public")); 
app.use(bodyParser.urlencoded({ 
    extended: true
})); 
  
app.post('/sign_up', function(req,res){ 
    var name = req.body.name; 
    var email =req.body.email; 
    var pass = req.body.password; 
    var phone =req.body.phone; 
  
    var data = { 
        "name": name, 
        "email":email, 
        "password":pass, 
        "phone":phone 
    } 
db.collection('details').insertOne(data,function(err, collection){ 
        if (err) throw err; 
        console.log("Record inserted Successfully"); 
              
    }); 
          
    return res.redirect('signup_success.html'); 
}) 
  
  
app.get('/', (req,res) => {   res.render('index.html');
    }).listen(3000) 
  
  
console.log("server listening at port 3000");
jyztefdp

jyztefdp1#

如果在访问http://127.0.0.1:3000/时遇到问题并得到此错误Cannot GET/index.html,请在var app=express()行之后将下面的行添加到app.js。
app.use(express.static(__dirname));
“Cannot GET/index.html”错误将得到解决。
app.js

var express=require("express"); 
var bodyParser=require("body-parser"); 
  
const mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost:27017/gfg', { useNewUrlParser: true, useUnifiedTopology: true }); 
var db=mongoose.connection; 
db.on('error', console.log.bind(console, "connection error")); 
db.once('open', function(callback){ 
    console.log("connection succeeded"); 
}) 
  
var app=express() 
app.use(express.static(__dirname));
  
  
app.use(bodyParser.json()); 
app.use(express.static(__dirname + "/../public")); 
app.use(bodyParser.urlencoded({ 
    extended: true
})); 
  
app.post('/sign_up', function(req,res){ 
    var name = req.body.name; 
    var email =req.body.email; 
    var pass = req.body.password; 
    var phone =req.body.phone; 
  
    var data = { 
        "name": name, 
        "email":email, 
        "password":pass, 
        "phone":phone 
    } 
db.collection('details').insertOne(data,function(err, collection){ 
        if (err) throw err; 
        console.log("Record inserted Successfully"); 
              
    }); 
          
    return res.redirect('signup_success.html'); 
}) 
  
  
app.get('/', (req,res) => {   res.render('index.html');
    }).listen(3000) 
  
  
console.log("server listening at port 3000");

index.html

<!DOCTYPE html>
<html>
<head>
    <title> Signup Form</title>
        
        
<link rel="stylesheet"
href=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity=
"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">    
        
<link rel="stylesheet" type="text/css" href="style.css">
        
</head>
<body>
    
    <br>
    <br>
    <br>
    <div class="container" >
        <div class="row">
        <div class="col-md-3">
                    
        </div>
                
        <div class="col-md-6 main">
                    
            <form action="/sign_up" method="post">
                    
            <h1> Signup form </h1>
                            
            <input class="box" type="text" name="name" id="name"
            placeholder="Name" required /><br>
                            
            <input class="box" type="email" name="email" id="email"
            placeholder="E-Mail " required /><br>
                            
            <input class="box" type="password" name="password"
            id="password" placeholder="Password " required/><br>
                            
            <input class="box" type="text" name="phone" id="phone"
            placeholder="Phone Number " required/><br>
                        <br>
            <input type="submit" id="submitDetails"
            name="submitDetails" value="Submit" /><br>
                    
            </form>
                    
        </div>
                
                
        <div class="col-md-3">
        </div>
                
    </div>
    </div>
</body>
</html>
p4rjhz4m

p4rjhz4m2#

因此,您的问题是您正在使用render作为视图。但是,您还没有指定渲染引擎(ejs、pug等)。
话虽如此,HTML文件不需要渲染引擎。你可以简单地像这样服务它们:

app.get('/', (req, res) => {
   res.sendFile('index.html', {
     root: './'
   })
})

相关问题