I’m currently building a web app using Node.js and Express.js.
I’m looking for a way to have a simple server-side authentication with a username and password in my main app.js file which is listening for a post request at http://www.domain.com/login:
app.js
app.post('/login', function(req, res) {
// some server-side code for a username and password
}
On the client-side I have a simple login form with a username and password. This form is going to posted to the server:
index.html
<form method="post" action="/login">
<input type="text" id="user" name="user" />
<input type="password" id="pass" name="pass" />
<button type="submit">Login</button>
</form>
I am looking to achieve this without the use of any ExpressJS plugins.
Edit: So my question is, how do I achieve a simple username and password authentication in ExpressJS and NodeJS?
I found the answer I was looking for using the
Connectmiddleware included withExpressJS, without the need to use cookies or add in another Node module:After running the application and navigating to
http://localhost:8080, it prompts for a username and password. Simples.