繁体   English   中英

无法获取 /index.html

[英]Cannot GET /index.html

我的项目目录是 -

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

我的应用程序文件代码如下 -

app.js 文件代码:-

var express=require("express");
var bodyParser=require("body-parser");

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/gfg');
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('public'));
//app.use(express.static(__dirname + "/../public"));
//app.use(app.router);
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('/',function(req,res){
res.set({
    'Access-control-Allow-Origin': '*'
    });
return res.redirect('index.html');
}).listen(3000)


console.log("server listening at port 3000");

当我运行 app.js 文件时,我在浏览器上得到“Cannot GET /index.html”? 在此处输入图像描述

您指定express.static为您的静态 html 文件提供服务。 此处查找快速静态文档。

app.use(express.static('public'));

您将 express.static 指向名为“public”的文件夹。 通常的做法是使用公共文件夹来存储所有静态文件。 创建一个public/文件夹并将您的 .css 和 .html 文件移到那里。

谢谢我试过了,它工作正常谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM