0

i have this simple app.js using express JS\NODEJS

var express= require("express");
var path= require("path");
var app= express();    
app.use(express.static(__dirname));
var users=[ {   userName: "faizan",  password: "faizan",  email: "[email protected]"},
            {   userName: "ali",     password: "ali",     email: "[email protected]"},
            {   userName: "hussain", password: "hussain", email: "[email protected]"},
            {   userName: "hassan",  password: "hassan",  email: "[email protected]"},
];

app.get('/',function(request , response){
    response.sendFile(__dirname+ '/index.html');

});

app.get('/frmSignUp', function(request, response){
    users.push({
        userName: request.query["userName"],
        password: request.query["password"],
        email: request.query["email"]
    });
    response.sendFile(__dirname+ '/dashboard.html');
    //how can i show my user array items in my dashboard html page?? Question1

});

app.get('/frmSignIn',function(request, response)
{
    if(users.contains(request.query["userName"])) //this is invalid
// is there any way available to check userName in array that is this exists?
    response.sendFile(__dirname+ '/dashboard.html');

})

var server =app.listen(3010, function(){
    console.log("server running on port "+ server.address().port);
})

i have index SignUp SignIn and Dashboard HTML pages in root directory. i have question which are commented in above code please let me know is these thing possible for me to do??

1 Answer 1

1

To answer your questions:

  1. Use templates. Probably the easiest way to do this coming from existing html is to use ejs (npm install ejs first in your project root directory). Example:

    • app.js:

      app.set('view engine', 'ejs');
      
      // ...
      
      app.get('/frmSignUp', function(request, response) {
        users.push({
          userName: request.query["userName"],
          password: request.query["password"],
          email: request.query["email"]
        });
        response.render('dashboard.ejs', { users: users });
      });
      
    • dashboard.ejs:

      <html>
        <head>
          <title>Dashboard</title>
        </head>
        <body>
          <h1>Users</h1>
          <ul>
          <% users.forEach(function(user) { %>
            <li><%= user.userName %></li>
          <% }); %>
          </ul>
        </body>
      </html>
      
  2. Since you have an array of objects and you're checking against a userName string, you'd need to manually loop over the users array and compare userNames.

Sign up to request clarification or add additional context in comments.

3 Comments

can you define first part of answer more!
thank you very much but one modification require is that make a folder in root (named views) and place dashboard.js there
You can change the views path if you want something different. The relevant app setting is views.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.