Answer the question
In order to leave comments, you need to log in
Why does it give a cannot get error?
I want to make sure that in case of successful insertion of data into the database, the user is redirected to another page. But for some reason, get cannot and the file name constantly write to me, although the server and the file to which I want to transfer are in the same folder. Can you please tell me why?
let express = require("express");
let app = express();
let port = 3000;
let bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
let mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost:27017/test");
let nameSchema = new mongoose.Schema({
name: String
});
let User = mongoose.model("User", nameSchema);
app.get("/", (req, res) => {
res.sendFile(__dirname + "/registr&login.html");
});
app.post("/addname", (req, res) => {
let myData = new User(req.body);
myData.save()
.then(item => {
res.redirect("/cA.html");
})
.catch(err => {
res.status(400).send("Что-то пошло не так");
});
});
app.listen(port, () => {
console.log("Server listening on port " + port);
});
Answer the question
In order to leave comments, you need to log in
to give statics by direct url, including html, you must first connect the express.static middleware specifying the folder where these static files are located, i.e. in this case, it is better to move the html, for example, to the public folder, then use it to return the static:
and add the path to the folder in the redirect:
this is if the redirect is needed for the static.
from a more correct option - add a route to get "/ca" and give a file to the request, as well as to "/", in order to give one file at a time, and not share all the statics
well, and the 3rd option - instead of a redirect, immediately give the file, here already depending on what you need.
app.use(express.static('public'))
res.redirect("/public/cA.html");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question