Categories
JavaScript Answers

How to fix the NPM “ENOENT: no such file or directory error” when installing Sails.js dependencies error with Node?

To fix the NPM "ENOENT: no such file or directory error" when installing Sails.js dependencies error with Node, we remove the package-lock.json file.

We remove the package-lock.json file and then re-run

npm install

to fix the error.

Categories
JavaScript Answers

How to include CSS files using Node, Express, and ejs?

To include CSS files using Node, Express, and ejs, we serve them as static files.

For instance, we write

app.use(express.static(__dirname + "/public"));

to serve the /public folder as a static files folder with express.static.

Then we add the css file in our ejs template with

<link rel="stylesheet" type="text/css" href="css/style.css" />
Categories
JavaScript Answers

How to implement login auth in Node.js?

To implement login auth in Node.js, we add a middleware to check for authentication and a login and logout route.

For instance, we write

const checkAuth = (req, res, next) => {
  if (!req.session.userId) {
    res.send("You are not authorized to view this page");
  } else {
    next();
  }
};

app.get("/my_secret_page", checkAuth, (req, res) => {
  res.send("You are logged in");
});

app.post("/login", (req, res) => {
  const post = req.body;
  if (post.user === "john" && post.password === "johnspassword") {
    //...
    req.session.userId = userId;
    res.redirect("/my_secret_page");
  } else {
    res.send("Bad user/pass");
  }
});

app.get("/logout", (req, res) => {
  delete req.session.userId;
  res.redirect("/login");
});

to define the checkAuth middleware function to check for the userId property in the session.

If it’s set, then the user is logged in and we call next.

Otherwise, we call res.send to return an error response.

Then we add the /my_secret_page route with app.get.

We call checkAuth before calling the route handler to check for authentication before sending the response.

Next we add the /login route that checks for the user and password from the req.body request body.

If they match, then we set req.session.userId since the user is authenticated.

And we call redirect to the /my_secret_page route.

Otherwise, we send an error response.

Finally, we add a /logout route that deletes the userId from req.session and redirect to /login

Categories
JavaScript Answers

How to run app inside Docker as non-root user?

To run app inside Docker as non-root user, we add a user that doesn’t require a password to log in.

For instance, we run

adduser --disabled-password --gecos '' r
adduser r sudo
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
su -m r -c /home/r/script.sh

to add the user with adduser.

We add it to the list of users that can run sudo with adduser r sudo.

Then we run the command we want with su -m r -c /home/r/script.sh.

Categories
JavaScript Answers

How to add multiple optional route parameters in Node Express?

To add multiple optional route parameters in Node Express, we add question marks after the parameter placeholders.

For instance, we write

app.get("/articles/:year?/:month?/:day?", (req, res) => {
  const year = req.params.year;
  const month = req.params.month;
  const day = req.params.day;
  //...
});

to add a get route handler with the year, month and day route parameters made option with ?.

We get the route parameter values from the req.params object.