简体   繁体   中英

A page can not be found on Node.JS server

So, I have a registration form. Whenever one user presses the submit button, I want that user to be redirected to another page called (menus.html) and also, when the user gets redirected, I want to run a query on the server side and write his username and password in the database. Unfortunately, this doesn't work because (menus.html) can not be found. So why is menus.html not on the server, but login.html is? My error is :

ERROR : http://127.0.0.1:3000/menus.html - 404 not found

Front-end side :

$("#submit_reg").click(function(){
        var username = $("#usr").val();
        var password = $("#pwd").val();

        $.ajax({
          url : "menus.html",
          type : "POST",
          data : {
             user: username,
             pass: password,
          },
          success : function(data){
             console.log("The data :) " + data + " :) " + "has been posted to menus.html");
          }
        });
 });

Server side :

var app = require('express')();
var mysql = require('mysql');
var bodyParser = require('body-parser');
var db = mysql.createConnection({
    host : "127.0.0.1",
    user : "root",
    password : "",
    database : "user_data",
    port : 3306
});

db.connect();

app.get('/',function(req, res){
  res.sendFile(__dirname+'/login.html');
});
app.post('/menus',function(req, res){
  res.sendFile(__dirname+'/menus.html');
});
app.listen(3000);

In your client-side script, you're posting to menus.html , not /menus .

You don't have anything set up to actually serve static contents, aside from your explicitly defined GET handler for / , which sends login.html . The only thing you've defined for menus is /menus , which is different than /menus.html .

What you should do is use the Express static module for your static files, and then define your post for /menus (or whatever path you want) from there, as you have started to do.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM