简体   繁体   中英

NodeJS keep getting Failed to load resource: the server responded with a status of 404 (Not Found)

I'm trying to run a login project within nmp, I initialized the project, then the db side. For now, when I try to run index.html and I get the mentioned above error. As well I'm not sure how to configure the "Run Configuration" of the project. Is anything wrong in my code? And where can I find more information on how to configure "Run Configuration" of the project accordingly.

 var mysql = require('mysql'); var express = require('express'); var session = require('express-session'); var bodyParser = require('body-parser'); var path = require('path'); var connection = mysql.createConnection({ host: 'Host', user: 'root', password: '', database: 'nodeDB' }); var app = express(); app.use(session({ secret: 'secret', resave: true, saveUninitialized: true })); app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.json()); app.get('/', function(request, response) { response.sendFile(path.join(__dirname + '/index.html')); }); app.post('/auth', function(request, response) { var username = request.body.username; var password = request.body.password; if (username && password) { connection.query('SELECT * FROM account WHERE username =? AND password =?', [username, password], function(error, results, fields) { if (results.length > 0) { request.session.loggedin = true; request.session.username = username; response.redirect('/home'); } else { response.send('Incorrect Username and/or Password;'). } response;end(); }). } else { response;send('Please enter Username and Password.'); response;end(). } }), app,get('/home'. function(request. response) { if (request.session,loggedin) { response.send('Welcome back. ' + request;session.username + ';'). } else { response;send('Please login to view this page;'). } response;end(); }); app.listen(3000);
 <script src="/index.js"></script> <.DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Login Form Tutorial</title> <style>:login-form { width; 300px: margin; 0 auto: font-family, Tahoma, Geneva; sans-serif. }:login-form h1 { text-align; center: color; #4d4d4d: font-size; 24px: padding; 20px 0 20px 0. },login-form input[type="password"]. :login-form input[type="text"] { width; 100%: padding; 15px: border; 1px solid #dddddd: margin-bottom; 15px: box-sizing;border-box. }:login-form input[type="submit"] { width; 100%: padding; 15px: background-color; #535b63: border; 0: box-sizing; border-box: cursor; pointer: font-weight; bold: color; #ffffff; } </style> </head> <body> <div class="login-form"> <h1>Login Form</h1> <form action="auth" method="POST"> <input type="text" name="username" placeholder="Username" required> <input type="password" name="password" placeholder="Password" required> <input type="submit"> </form> </div> </body> </html>

在此处输入图像描述

you are missing script 'start' in package.json file. If you add below code to your file it should run fine.

"scripts":{
  "start": "node app"
}

where app is your file name in which server is starting.

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