

`app.get("/public/list")` には新しいページが表示させたいけど、なぜ動かないか知らない。

[code]

     app.get("/",(req,res)=>{
       res.sendFile(__dirname+"/public/index.html")
      })
    //ここは問題があるかも
      app.get("/public/list", (req,res)=>{
        console.log("Attempting to access next page....")
        res.sendFile(path.join(__dirname,"public", "list", "index.html"),{headers: {"Content-Type": "application/json"}}, function(err){
          if(err){
            console.log(err)
          }  else {console.log("SENT")}
        })
        
      })
    
    myDB(async client =>{
      
      const myDataB = await client.db('database').collection('userz');
      
      apiRoutes(app, myDataB);
      
    //routes
       app.post("/register", (req,res,next)=>{
        console.log(req.body)
        myDataB.findOne({username: req.body.username}, (err, user)=>{
          if(err){
            console.log("err");
            next(err);
          } else if(user){
            console.log("already user")
            res.redirect("/");
          } else {
            console.log("making new user")
            myDataB.insertOne({
              username: req.body.username,
              password: req.body.password
            }, (err, doc)=>{
              if(err){
                console.log(err)
                res.redirect("/");
              } else{
                 next(null, doc)
                }
              })
            }
        })
      }, passport.authenticate("local", {failureRedirect: "/", successRedirect: path.join(__dirname, "public", "list")}));
    
      app.post("/login", passport.authenticate("local", {
        failureRedirect: "/", 
        successRedirect: path.join("public","list"),
        failureFlash: false}));
      
    //passport stuff
    
      //my strategy for authenticating users on login
      passport.use(new localStrategy((user,pass,done)=>{
        myDataB.findOne({username: user}, function(err, u){
          console.log("Looking for user....");
          if(err){
            return done(err)
          };
          if(!u){
            console.log("no user")
            return done(null, false)
          };
          if(u.password !== pass){
            console.log("wrong password")
            return done(null, false)
          }
          console.log("User found...")
          return done(null, u);
        })
      }));
    
    passport.serializeUser((user, done) => {
        console.log("Serialize")
        done(null, user._id);
      });
      
      passport.deserializeUser((id, done) => {
        console.log("Deserialize")
        myDataB.findOne({ _id: new ObjectID(id) }, (err, doc) => {
          if(err){
            return done(err)
          }
          console.log(doc)
          console.log("Deserialize success")
          return done(null, doc);
        });
      });
    
    }).catch(e =>{
      console.log(e);
    }) 
    app.listen("3000", function(){
      console.log("connected!...")
    })
    
[/code]

[code]

    <body>
      <h1>Please login or register</h1>
      <form id="formLogin">
        <label>LOGIN</label>
        <input type="text" placeholder="Username" id="logValue"/>
        <input type="text" placeholder="Password" id="logpassvalue"/>
        <input type="submit" value="SUBMIT" />
      </form>
      <form id="formRegister">
        <label>REGISTER</label>
        <input type="text" placeholder="Username" id="registerValue"/>
        <input type="text" placeholder="Password" id="passValue"/>
        <input type="submit" value="SUBMIT" />
      </form>
    
      <script>
        document.getElementById("formRegister").addEventListener("submit", (event) => {
          const user = document.getElementById("registerValue").value;
          const pass = document.getElementById("passValue").value;
          event.preventDefault();
        fetch("/register", {
          method: "POST",
          headers: {
            "Content-Type": "application/json"
          },
          body: JSON.stringify({username:user, password: pass })
        })
        .then(response => response.text())
        .catch(error => console.log(error))
        });
        
        document.getElementById("formLogin").addEventListener("submit", (event)=>{
          const user = document.getElementById("logValue").value;
          const pass = document.getElementById("logpassvalue").value;
          event.preventDefault();
          fetch("/login", {
          method: "POST",
          headers: {
            "Content-Type": "application/json"
          },
          body: JSON.stringify({username:user, password: pass })
        })
        .then(response => response.text())
        .then(data => console.log(data))
        .catch(error => console.log(error))
        });
          
      </script>
    </body>
    
[/code]


CC BY-SA 4.0
2023-01-24T04:08:55.677
93539
2023-01-24T07:37:20.580
2023-01-24T07:37:20.580
3060
56743
question
javascript node.js express.js
res.sendFile()がhtmlを送らないでなぜか知らない。