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? enter image description here
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.
Thank you I have tried and its working fine thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.