4

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>

enter image description here

Victoria
  • 165
  • 1
  • 12
  • 1
    did you try node index,js ? – Engineer S. Saad Mar 17 '20 at 11:25
  • your works fine for me !!! it shows login page !! – Engineer S. Saad Mar 17 '20 at 11:36
  • 1
    How are you trying to access the page? Is it "http://localhost:3000/index.html"? This is not supposed to work, you can access normally on the / endpoint, like this: "http://localhost:3000/" – desoares Mar 17 '20 at 11:38
  • 2
    i think she is having issue with her IDE but otherwise her code is working fine !!! try to use Visual Studio Code for Node js it is easy to use – Engineer S. Saad Mar 17 '20 at 11:41
  • 1
    Thx @SaadSohail basically "node index.js" worked fine now! – Victoria Mar 17 '20 at 11:51
  • 1
    Thx @desoares "localhost:3000" works fine! – Victoria Mar 17 '20 at 11:52
  • Basically this issue was solved by: 1) First in Terminal run "node index.js" 2) Opening "localhost:3000" in browser 3) And for "Run Configuration" I added in package.json the mentioned code.Then in "Run/Debug Configurations" I added npm, then in scripts label "start" and it works fine for me. "scripts": { "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, – Victoria Mar 17 '20 at 12:42

1 Answers1

1

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.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Furqan Ali
  • 11
  • 1