1

I want to create html content that looks something like this using node.js.

<div class="outputs">
   ...
</div>

I have the following code:

var mongoose = require("mongoose");
var express = require("express");
var bodyParser = require("body-parser");
var Url = require("./models/Url");
var shortId = require("shortid");
var http = require("http");
var app = express();
var { JSDOM } = jsdom;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.connect(process.env.MLAB_URI);

app.get("/urls", (req, res, next) => {
  Url.find({}, function(err, data) {
    res.json(data);
    console.log(data.length);
  });
});

app.get("/deletebase", (req, res, next) => {
  Url.deleteMany({}, function(err, data) {
    res.json(data);
  });
});

app.use(express.static(__dirname + "/"));

app.get("/:shortUrl", function(req, res, next) {
  Url.findOne({ shortUrl: req.params.shortUrl }, function(err, findUrl) {
    if (err) console.log(err);
    if (!findUrl) {
      return next({ status: 400, message: "unknown shorturl" });
    }
    res.redirect(findUrl.longUrl);
  });
});

app.post("/", function(req, res) {
  var url = new Url(req.body);
  var hostname = req.headers.host;
  var expression = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi;
  var regex = expression;
  if (regex.test(url) === true) {
    url.shortUrl = shortId.generate();
    url.fullUrl = "https://" + hostname + "/" + url.shortUrl;
    url.save(function(err, savedUrl) {
      if (err) console.log(err);
      res.redirect("https://" + hostname);
    });
  } else {
    res.redirect("https://" + hostname);
  }
});

var options = {
  runScripts: "dangerously",
  resources: "usable"
};

app.listen(3000, function() {
  console.log("RUNNING");
});

I want to get length of the data and create that many div objects with longUrl and shortUrl objects in it. Also when database will be updated new div object should be created, and when I delete database information all the div elements should be deleted too, is this possible to do?

2
  • I don't fully understand the logic here, it appears your POSTing then generating a shorturl and redirecting to it, but then on the shorturl route you're redirecting to itself, I think you should write the HTML output here instead. Commented Jan 8, 2020 at 14:19
  • see stackoverflow.com/a/44882366/271433 for examples on how to output a HTML response. Commented Jan 8, 2020 at 14:19

1 Answer 1

1

You should be using a templating engine for this the two most popular ones for Node.js are pug(formerly Jade) and hbs(Handlebars.js).

There are a lot of other template engines here you could consider.

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

2 Comments

I have index.html in my project where is stored everything, and i just want to edit it with node.js and add some div objects
I personally like ejs because it is so similar to PHP. ejs.co

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.