ExpressJS & Websocket & session sharing

I found this works for me. Not sure it’s the best way to do this though. First, initialize your express application: // whatever your express app is using here… var session = require(“express-session”); var sessionParser = session({ store: session_store, cookie: {secure: true, maxAge: null, httpOnly: true} }); app.use(sessionParser); Now, explicitly call the session middleware from … Read more

Error [ERR_REQUIRE_ESM]: require() of ES Module … not supported

The current version of node-fetch is ONLY compatible with an ESM import (using import), not from CommonJS modules using require(). You have these choices to fix: Switch your project to an ESM module and load it with import fetch from ‘node-fetch’;. In a very recent version of nodejs, you can dynamically import an ESM module … Read more

Sequelize – SQL Server – order by for association tables

From Sequelize offical docs: // Will order by an associated model’s created_at using an association object. (preferred method) [Subtask.associations.Task, ‘createdAt’, ‘DESC’], // Will order by a nested associated model’s created_at using association objects. (preferred method) [Subtask.associations.Task, Task.associations.Project, ‘createdAt’, ‘DESC’], By referring above syntax, Update your order option like below User.findById(uID, { include: [{ model: sequelize.models.userProfile … Read more

Modify existing Excel File using node.js

exceljs does let you modify Excel spreadsheets. Here’s an example of reading in an existing spreadsheet and writing it back out to a different file: var Excel = require(‘exceljs’); var workbook = new Excel.Workbook(); workbook.xlsx.readFile(‘old.xlsx’) .then(function() { var worksheet = workbook.getWorksheet(1); var row = worksheet.getRow(5); row.getCell(1).value = 5; // A5’s value set to 5 row.commit(); … Read more

Nodejs EJS helper functions?

Yes, in Express 3 you can add helpers to app.locals. Ex: app.locals.somevar = “hello world”; app.locals.someHelper = function(name) { return (“hello ” + name); } These would be accessible inside your views like this: <% somevar %> <% someHelper(‘world’) %> Note: Express 2.5 did helpers differently.