Search code examples
node.jsaccountdust.js

How do I make accounts system with Dust?


I really hate php, so I've looked up for node.js render engines and found Dust, and it is really what I am looking for with postgres. However, I haven't found something similar to $_SESSION of php. Is there a way for this or I am picking the wrong tool?


Solution

  • Dust.js is a templating language. PHP can be used as a templating language too but is also a full-blown programming language that is primarily used for creating Web applications. That's why it has a very easy way to initiate and store data about sessions – $_SESSION.

    Dust.js, on the other hand, is only a templating language that can be compiled down to HTML. It is not a programming language and also doesn't provide a web server. If you want to store session data, you need a web framework like Express. It allows you to use Dust.js as a templating language and store session data.

    However, I personally would – especially since you don't seem to have a special reason to use Dust.js above other templating languages, recommend trying out Pug.js. It's – contrary to Dust.js – actively maintained, used by a lot of developers, has a very nice syntax and can be activated in Express with just one line of code.

    Edit: express-session example usage

    const express = require('express')
    const session = require('express-session')
    
    const app = express()
    
    app.use(session({
      secret: 'keyboard cat',
      resave: false,
      // This option automatically initiates a session for every visitor
      saveUninitialized: true
    }))
    
    app.get('/', function (req, res, next) {
      if (!req.session.views) req.session.views = 0
      req.session.views++
      res.send(`You viewed this page ${req.session.views} times`)
    })