Categories
JavaScript Answers

How to run a Node express server using webpack-dev-server?

To run a Node express server using webpack-dev-server, we run the webpack-dev-server command.

For instance, we write

const express = require("express");
const app = express();

require("./_routes")(app);
app.set("port", 8080);
app.listen(app.get("port"), () => {
  console.log("Node App Started");
});

to add an Express app in dev-server.js

Then in package.json, we write

{
  "scripts": {
    "start": "webpack --progress --colors",
    "dev-server": "node dev-server.js",
    "dev-client": "webpack-dev-server --port 3000"
  }
}

to add the dev-client script to run "webpack-dev-server in the dev-client script on port 3000.

Categories
JavaScript Answers

How to create Node.js server that accepts POST requests?

To create Node.js server that accepts POST requests, we use the http module.

For instance, we write

const http = require("http");
const server = http.createServer((request, response) => {
  response.writeHead(200, { "Content-Type": "textplain" });
  if (request.method === "GET") {
    response.end("received GET request.");
  } else if (request.method === "POST") {
    response.end("received POST request.");
  } else {
    response.end("Undefined request .");
  }
});

server.listen(8000);

to call createServer with a callback that checks the request method with the request.method property.

If it’s 'POST', then a post request is received.

Categories
JavaScript Answers

How to create rooms in Node Socket.io?

To create rooms in Node Socket.io, we call in and join.

For instance, on client side, we write

const socket = io.connect();
socket.emit("create", "room1");

to call emit to emit the create event with the room name.

On server side, we write

io.sockets.on("connection", (socket) => {
  socket.on("create", (room) => {
    socket.join(room);
  });
});

to call on to listen for the create event and call join to create the room.

Then we can send messages to the room with

io.sockets.in(room).emit("event", data);

by call in with the room name.

Categories
JavaScript Answers

How to fix Error: EACCES: permission denied when trying to install ESLint using npm?

To fix Error: EACCES: permission denied when trying to install ESLint using npm, we set a few options.

For instance, we run

sudo npm install -g eslint --unsafe-perm=true --allow-root

to install eslint with npm install with --unsafe-perm=true and --allow-root to install as root and run with root permissions.

Categories
JavaScript Answers

How to add Node Express Passport error handling?

To add Node Express Passport error handling, we use the middleware returned by passport.authenticate.

For instance, we write

app.post(
  "/login",
  passport.authenticate("local", {
    successRedirect: "/loggedin",
    failureRedirect: "/login",
    failureFlash: true,
  })
);

to call passport.authenticate to return the authentication middleware.

We use that as the argument of post to make it use Passport authentication.

We set the successRedirect and failureRedirect properties to set the redirect destination in both cases.