0

I am currently working with node.js doing a Login and Register page, however i am trying to get my data to be shown in a clean html presentation,

Here is my code: Register

var Cryptr = require('cryptr');
var express=require("express");
const mysql = require('mysql');

let con = mysql.createConnection({
  host: "----",
      user: "---",
      password: "----",
      database: "---"
});

module.exports.register=function(req,res){
    var today = new Date();
  var encryptedString = cryptr.encrypt(req.body.password);
    var userss={
        "name":req.body.name,
        "email":req.body.email,
        "password":encryptedString,
        "created_at":today,
        "updated_at":today
    }
    con.query('INSERT INTO userss SET ?',userss, function (error, results, fields) {
      if (error) {
        res.json({
            status:false,
            message:'there are some error with query'
        })
      }else{
          res.json({
            status:true,
            data:results,
            message:'user registered sucessfully'
        })
      }
    });
}

My data shown on my page: Result

But I would like to put it in a clean html format,

Thank you in advance for your help!

6
  • app.set('json spaces', 2); where const app = express(); Commented Mar 16, 2019 at 17:38
  • 1
    Your result is not HTML, just JSON format. If you want true HTML, use a template engine like PUG. Commented Mar 16, 2019 at 17:40
  • @Rashomon I guess what he wants is a clean JSON output, but not for sure :) Commented Mar 16, 2019 at 17:42
  • @BrianLe Well, for me the title is clear :P Commented Mar 16, 2019 at 17:42
  • My bad, didn't even read the title properly @Rashomon Commented Mar 16, 2019 at 17:43

1 Answer 1

1

The response you are sending is in JSON format .If the HTML pages you wish to send are small and simple, then the following would work perfectly fine:

 res.send('<html>
 <p>The status is TRUE</p>
 <p>The data is '+results+'</p>
 <p>The message is User registered successfully
</html>');

However,as you might have guessed, this will get tedious for larger/complex pages which is why you use a template engine which makes HTML pages for you. A list of them can be found at: https://expressjs.com/en/resources/template-engines.html

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

Comments

Your Answer

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