HTML Links in Node JS – JavaScript – SitePoint Forums

I have a string template literal to dynamically create an html file, but if I try to add a link tag with href in the string and run Node to run the code to create the html file, I get a 404 error because it’s trying to call GET on my HTML string while it’s building the file. Is there a way to include the href=”path/to/something” in the string without trying to get it?


Can you show us the code you use to do this?

It was when I added the in the string template element that the node tries to get the styles.css file instead of just writing it to the html document.

const csvFilePath = "csv/FormSubmissions.csv";
const csv = require("csvtojson");

const PdfWriter = require("csv-converter-to-pdf-and-html/modules/PdfWriter");

const path = require("path");
const fs = require("fs");
const fsPromises = fs.promises;

const pdfFilePath = "./Form-Submissions.pdf";

const creatHtmlFile = async (data) => {
  const fileName = "./Form-Submissions.html";
  return await fsPromises.writeFile(fileName, data);
};

// CREATE HTML FROM JSON OBJECT
const createSubmissionHtml = async (JsonObj) => {
  try {
    return `
      
      
          
          Form Submissions
          
          
          
              ${Object.keys(JsonObj).map((key) => {
                let submission = JsonObj[key];
                return `
                  
    ${Object.entries(submission) .map((entry) => { let listItem; const groups = [ "Contact", "Founders", "Progress", "Idea", "Equity", "Legal", "Curious", ]; const [prop, value] = entry; const propertyValue = `
  • ${prop}:
    ${value}
  • `; const sectionGroup = `
  • ${prop}:
  • `; groups.includes(prop) ? (listItem = sectionGroup) : (listItem = propertyValue); return listItem; }) .join("n")}
`; })} `; } catch (error) { console.log(error); } }; csv({ delimiter: ",", }) .fromFile(csvFilePath) .then((JsonObj) => createSubmissionHtml(JsonObj)) .then((html) => { creatHtmlFile(html); PdfWriter.WritePDF(pdfFilePath, html); }) .catch((err) => err.message);

There’s nothing obvious that I can see that would cause Node to do this.

Unfortunately, in the code you posted, there are a few puzzle pieces that I don’t have access to (e.g. FormSubmissions.csv).

Could you:

  • remove all extraneous steps (e.g. PDF generation is probably irrelevant to the problem to be solved)
  • hardcode any missing values ​​in the code (e.g. the CSV file can probably be replaced with a hardcoded file JsonObj variable)
  • Posting enough code that I can reproduce the problem on my PC.

If you can do that, I don’t mind taking a look and helping you figure out why this behavior is happening.



1 like

Source link

About Irene S. Stroupe

Check Also

COD Adds Web Development Degree

GLEN ELLYN — The College of DuPage said Monday that it now offers an associate’s …