Skip to main content

Posts

Showing posts with the label express

Handle File Upload with Express and Multer

Express undoubtedly has become a popular framework for building web applications based on Node.js. It is shipped with support for handling file uploading using middleware that takes the user requests, parses the contents for the files, and provides the next handler with the files information. The following snippet shows a basic example of handling files uploading in Express using Multer. const multer = require('multer'); const storage = multer.diskStorage({ destination: (req, file, cb) => { // store files to "uploads/" directory cb(null, 'uploads/'); }, }); const upload = multer({ storage }); // initiate an upload handler that can accept multiple fields and multiple files const uploadHandler = upload.fields([ { name: 'galleryImages', maxCount: 10 }, { name: 'userFiles', maxCount: 2 }, ]); app.post('/upload', uploadHandler, (req, res) => { res.json(req.files); }); The sample above can be used if we just...

Create A GraphQL Server Using Express and Apollo Server

GraphQL can be described as a query language for API. GraphQL was initiated by Facebook developers when they tried to build a better data fetching mechanism for their mobile application. By using GraphQL, frontend developers can request any data from the backend server with specified format and properties based on their actual needs. Creating a server that has support for handling GraphQL-based requests has become easy nowadays. Express as a de-facto framework for building HTTP server has the capability to be integrated with Apollo Server which is a popular GraphQL server. Minimal modules that we need are express , graphql , and apollo-server-express . For instance, we will set up a project and build the requirements. We utilize ESM syntax for building the sample program. Firstly, we initiate the project and install the dependencies. mkdir express-graphql cd ./express-graphql yarn init -y yarn add express graphql apollo-server-express Because we use ESM syntax, we need to set the...