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?
asked Nov 14, 2021 at 19:10
Akash Kumar
291 gold badge2 silver badges8 bronze badges
1 Answer 1
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
Andre
4,6955 gold badges24 silver badges35 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Akash Kumar
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.
default