Categories
JavaScript Answers

How to fix nodemon not found in npm with JavaScript?

To fix nodemon not found in npm with JavaScript, we install nodemon.

To install it, we run

npm install nodemon --save 

Then we add

{
  //...
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon app.js"
  }
  //...
}

in package.json to add the start script to run app.js with nodemon.

Then we run

npm start

to start the script.

Categories
JavaScript Answers

How to use socket.io in Express 4 and express-generator’s /bin/www with JavaScript?

To use socket.io in Express 4 and express-generator’s /bin/www with JavaScript, we create a module with the socket.io code.

For instance, we write

const io = require("socket.io")();
const socketApi = {
  io,
};

io.on("connection", (socket) => {
  console.log("A user connected");
});

module.exports = socketApi;

in socketApi.js to listen for client connections by call on with 'connection' and the event handler.

We export the socketApi object with the io object.

Then we write

const app = require("../app");
const debug = require("debug")("socketexpress:server");
const http = require("http");
const socketApi = require("../socketApi");

const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);

const server = http.createServer(app);
socketApi.io.attach(server);

to import the socketApi file with require("../socketApi").

Then we call socketApi.io.attach to attach the Express server to it.

Categories
JavaScript Answers

How to save multiple documents concurrently in Mongoose and Node.js?

To save multiple documents concurrently in Mongoose and Node.js, we call the create method.

For instance, we write

const array = [{ type: "jelly bean" }, { type: "snickers" }];
Candy.create(array, (err, jellybean, snickers) => {
  if (err) {
    //...
  }
});

to call the create method with the array to write the entries into the collection.

And then we get the written entries from the parameters after err.

err is the error if there is any.

Categories
JavaScript Answers

How to clear console on Node.js on windows?

To clear console on Node.js on windows, we call the proces.stdout.write method.

For instance, we write

process.stdout.write('\033c');

to call write with '\033c' to clear the console.

Categories
JavaScript Answers

How to include external .js file in Node.js app?

To include external .js file in Node.js app, we create a module.

For instance, we write

const make = (Schema, mongoose) => {
  CarSchema = new Schema({
    brand: String,
    type: String,
  });
  mongoose.model("Car", CarSchema);
};

module.exports.make = make;

in car.js to export the make function.

Then we use it by writing

const app = require("express").createServer();
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;

require("./models/car.js").make(Schema, mongoose);

We call require to require the car.js file and call make from the returned object.