Categories
JavaScript Answers

How to make Sequelize table without column ‘id’ with Node.js?

To make Sequelize table without column ‘id’ with Node.js, we call the removeAttribute method.

For instance, we write

AcademyModule = sequelize.define(
  "academy_module",
  {
    academy_id: DataTypes.INTEGER,
    module_id: DataTypes.INTEGER,
    module_module_type_id: DataTypes.INTEGER,
    sort_number: DataTypes.INTEGER,
    requirements_id: DataTypes.INTEGER,
  },
  {
    freezeTableName: true,
  }
);
AcademyModule.removeAttribute("id");

to define the AcademyModule model with define.

We map it to the academy_module table in the database.

We call removeAttribute to remove the id column.

Categories
JavaScript Answers

How to import node’s path module using import path from ‘path’ with Node.js?

To import node’s path module using import path from ‘path’ with Node.js, we add the type definitions for node.

And the we import the module.

To install the type definitions, we run

npm install --save-dev @types/node

Then we import path with

import * as path from 'path';

to import all members into the path object.

Categories
JavaScript Answers

How to read file into a base64 string in Node.js?

To read file into a base64 string in Node.js, we call the readFile method.

For instance, we write

const fs = require("fs").promises;
const contents = await fs.readFile("/path/to/file.jpg", { encoding: "base64" });

to call the readFile method to read the /path/to/file.jpg file into a base64 string by setting the encoding option to 'base64'.

Categories
JavaScript Answers

How to add MySQL connection pooling with Node.js?

To add MySQL connection pooling with Node.js, we use the createPool method.

For instance, we write

const mysql = require("mysql");

const pool = mysql.createPool({
  host: "localhost",
  user: "root",
  password: "root",
  database: "guess",
});

pool.getConnection((err, connection) => {
  callback(err, connection);
});

to call createPool to create a connection pool.

Then we call pool.getConnection to get a connection and make queries with it.

Categories
JavaScript Answers

How to upload images using Node.js, Express, and Mongoose?

To upload images using Node.js, Express, and Mongoose, we use the express.multipart method.

For instance, we write

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

app.use(express.static("./public"));
app.use(express.methodOverride());
app.use(
  express.multipart({
    uploadDir: "./uploads",
    keepExtensions: true,
  })
);

app.use(app.router);

app.get("/upload", (req, res) => {
  res.render("upload");
});

app.post("/upload", (req, res) => {
  res.json(req.files);
});

http.createServer(app).listen(3000, () => {
  console.log("App started");
});

to call express.multipart to return a middleware function that accepts uploads.

And then we get the uploaded file from req.files in the /upload route handler.

We use express.static to expose the /public folder as a static file folder.