Home > Software design >  applying styling for markdown in express/nodejs
applying styling for markdown in express/nodejs

Time:11-22

I'm trying to load a markdown file as a static file which should afterwards be rendered via a html file. My question now is how I am to apply CSS Styling. Here is my code for the index.js:

const express = require('express');
const bodyparser = require('body-parser');
const showdown = require('showdown');
const marked = require('marked');

const app = express()

app.use(express.static(__dirname   '/'));
app.use(bodyparser.urlencoded({extend:true}));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname);
app.use(express.static('public'));

app.get('/article', (req,res)=> {
    var markdown = require("markdown-js");
    var fs = require("fs");

    var path =  __dirname 'articles/article.md'

    app.use(express.static("public"));

    fs.readFile(path, 'utf8', function(err, data) {
        if(err) {
          console.log(err);
        }
        //res.send(marked(""   data));
        //console.log(result);
        res.render('index.html');
      });
});

// listen to port
const PORT= 3000;

app.listen(process.env.PORT || 3000, function(){
    console.log('server is running on port 3000');
})

The code for my current html file looks the following such that index.html:

<!DOCTYPE html>
<html lang="en">
    <script src="style.css"></script>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <% result %>
</body>
</html>

CodePudding user response:

In order to use variables you have to use ejs you can read about it here https://ejs.co/

then you can do something like this:

  • First change the name to index.ejs
  • Then you have to pass the data
res.render("index", { result: data })
  • Related