Routes Auth.js
Routes With Files
server
├── index.js
import { register } from "./controllers/auth.js";
/* ROUTES WITH FILES */
app.post("/auth/register", upload.single("picture"), register);
File Storage
Before we start discussing the routes with files, let's first talk about file storage. In the code snippet below, we are defining a multer disk storage. Multer is a middleware that allows us to handle file uploads in Node.js.
/* FILE STORAGE */
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "public/assets");
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
const upload = multer({ storage });
The storage
object tells Multer where to store the uploaded files. In this case, we're storing them in the public/assets
directory. The filename
function defines how the file should be named. In this case, we're using the original name of the file. Finally, we create an instance of Multer with our storage object.
I already discuss about this in the previous chapter in details. If you want to know more about file storage, you can check it out here.
Routes with Files
Now that we have our file storage set up, we can move on to handling routes with files. In the code snippet below, we have an example route that handles file uploads.
import { register } from "./controllers/auth.js";
/* ROUTES WITH FILES */
app.post("/auth/register", upload.single("picture"), register);
This route handles POST requests to /auth/register. The upload.single("picture") middleware is responsible for handling the file upload. It specifies that we expect a single file with the field name picture. Finally, we pass the register function as the endpoint logic.
The register function is the logic for our endpoint. This function can access the uploaded file via the req.file object. The req.file object contains information about the uploaded file, such as the file name, file type, and file size. With this information, we can save the file to disk, store information about the file in a database, or perform any other necessary logic.
Conclusion
Handling file uploads in Node.js applications can be tricky, but Multer makes it easy. By defining a file storage and using the upload middleware, we can easily handle file uploads in our routes. With this knowledge, you can now handle file uploads in your own Node.js applications.
authRoutes
import authRoutes from "./routes/auth.js";
This is going to be our rout folder where we have the path and the routes for every types of features so in this case it would be authRoutes.
/* ROUTES */
app.use("/auth", authRoutes);
This will help us set up routes and keep our files organized and clean.
Routes Auth Setup
Server
├── routes
│ ├── auth.js
import express from "express";
import { login } from "../controllers/auth.js";
const router = express.Router();
router.post("/login", login);
export default router;
The auth.js file is a module that defines routes related to authentication in an Express application. Let's take a closer look at this file and understand how it works.
Importing Express and Controllers
The first thing we do in auth.js is import express from the express module. We also import login from the ../controllers/auth.js module.
import express from "express";
import { login } from "../controllers/auth.js";
Creating a Router
Next, we create a router instance using express.Router(). This allows us to define routes specific to the auth part of our application.
const router = express.Router();
Defining Routes
Now that we have created our router instance, we can define our routes. In this case, we are defining a POST route for the /login endpoint.
router.post("/login", login);
Exporting the Router
Finally, we export the router instance so that we can use it in our application.
export default router;