express.js 4 and sockets with express router

One option is to pass it in to req object. app.js: var express = require(‘express’); var path = require(‘path’); var logger = require(‘morgan’); var api = require(‘./routes/api’); var app = express(); var io = require(‘socket.io’).listen(app.listen(3000)); app.use(logger(‘dev’)); app.use(express.static(path.join(__dirname, ‘public’))); io.sockets.on(‘connection’, function (socket) { console.log(‘client connect’); socket.on(‘echo’, function (data) { io.sockets.emit(‘message’, data); }); }); // Make io … Read more

How to make an Express site without a template engine?

If what you want is directly to serve static html files with the possibility to cache resources, while still being able to hit “https://stackoverflow.com/” and get index.html, then the answer is as easy as this: var express = require(‘express’); var http = require(‘http’); var app = express(); app.use(express.static(__dirname + ‘/public’)); http.createServer(app).listen(3000); Gotcha: Html files including … Read more

node.js express createServer() not a function

Change: var express = require(‘express’); var app = module.exports = express.createServer(); To: var express = require(‘express’); var app = express(); //Middleware app.listen(3000) You can also globally install express with the following command and then automatically generate a express template with the following command: npm install -g express Generate template: express myAppName cd . && npm … Read more

TypeError: Failed to execute ‘compile’ on ‘WebAssembly’: Incorrect response MIME type. Expected ‘application/wasm’

One possible workaround is to use instantiate() instead of instantiateStreaming(), since the former doesn’t care about MIME types (while the latter does). To use instantiate(): async function fetchAndInstantiate() { const response = await fetch(“http://localhost:3000/simple.wasm”); const buffer = await response.arrayBuffer(); const obj = await WebAssembly.instantiate(buffer); console.log(obj.instance.exports.add(1, 2)); // “3” }

Handlebars with Express: different html head for different pages

This is a great question and, in my mind, a glaring weakness in Express’s view model. Fortunately, there is a solution: use Handlebars block helpers. Here’s the helper I use for this purpose: helpers: { section: function(name, options){ if(!this._sections) this._sections = {}; this._sections[name] = options.fn(this); return null; } } Then, in your layout, you can … Read more

Enabling CORS in Create React App utility

If you are needing this for development and wanting to access an api from your react app but getting an error like this- Failed to load http://localhost:8180/tables: The ‘Access-Control-Allow-Origin’ header has a value ‘http://localhost:8180’ that is not equal to the supplied origin. Origin ‘http://localhost:3000’ is therefore not allowed access. Have the server send the header … Read more

Express routes parameters

Basically, your declared routes are documented in the Express documentation. The second route is resolved by a URL like /api/choice/hello where ‘hello’ is mapped into the req object object as: router.get(‘/api/choice/:id’, function (req, res) { console.log(“choice id is ” + req.params.id); }); What you are actually trying is mapping query parameters. A URL like /api/choice/?id=1 … Read more

Angular and Express routing

Add these routes to your express server app.get(‘/partials/:filename’, routes.partials); app.use(routes.index); Then in routes.js exports.partials = function(req, res){ var filename = req.params.filename; if(!filename) return; // might want to change this res.render(“partials/” + filename ); }; exports.index = function(req, res){ res.render(‘index’, {message:”Hello!!!”}); }; This will make sure that express returns rendered templates when making requests to partials/index … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)