2

my project directory is -

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

my app file code are given below-

app.js file code:-

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");

when I am running the app.js file then I am getting "Cannot GET /index.html" on the browser?

Dharman
34k27 gold badges105 silver badges156 bronze badges
asked Nov 14, 2021 at 19:10

1 Answer 1

7

You specified express.static to serve your static html files. Find the express static documentation here.

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

You are pointing express.static to a folder called "public". It's common practice to have a public folder to store all your static files. Create a public/ folder and move your .css and .html files there.

answered Nov 14, 2021 at 19:14
Sign up to request clarification or add additional context in comments.

1 Comment

I have got the message "Cannot GET /index.html" because I was not providing the correct path of the HTML file. now after resolving it my project directory look like this- app ├── app.js ├── node_modules ├── package.json ├── package-lock.json └── public ├── index.html ├── signup_success.html └── style.css Thank you I have resolved my issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.