Quickstarters
CRUD Samples
How to Build a CRUD App with Node.js?
33 min
overview in this walkthrough, you will learn how to develop a complete crud (create, read, update, delete) application using node js we will utilize back4app as our backend service to simplify data management this guide will walk you through configuring a back4app project, designing a dynamic data model, and implementing crud operations using node js initially, you will set up a back4app project named basic crud app node which provides a robust, non relational data storage solution you will then design your data schema by defining collections and fields either manually or by leveraging the back4app ai agent next, you will manage your backend through the back4app admin app—a user friendly, drag and drop interface for data manipulation finally, you will integrate your node js application with back4app using the parse javascript sdk (or the rest/graphql api as needed) while ensuring secure access controls by the end of this tutorial, you will have built a production ready node js application capable of performing essential crud operations along with user authentication and data security what you will learn how to create a node js based crud application with an effective low code backend strategies for designing a scalable backend and connecting it with your node js app how to utilize the back4app admin app for streamlined create, read, update, and delete operations deployment techniques, including docker containerization, to efficiently launch your node js application prerequisites before diving in, make sure you have a back4app account with a freshly configured project need help? visit getting started with back4app https //www back4app com/docs/get started/new parse app a node js development setup use a code editor like visual studio code and ensure node js (v14 or later) is installed basic knowledge of node js, asynchronous programming, and rest apis refer to the node js documentation https //nodejs org/en/docs/ if needed step 1 – setting up your project creating a new back4app project log into your back4app account click on the “new app” button from your dashboard name your project basic crud app node and follow the on screen prompts to complete the setup create new project after creation, your project will appear on your dashboard, ready for backend configuration step 2 – crafting your data schema defining your data structures for this crud application, you will create several collections in your back4app project below are examples of the key collections and fields needed for basic crud functionality 1\ items collection this collection stores details about each item field type description id objectid auto generated unique identifier title string the name of the item description string a brief overview of the item createdat date timestamp marking when the item was added updatedat date timestamp marking the last update 2\ users collection this collection handles user credentials and authentication data field type description id objectid auto generated unique identifier username string unique name for the user email string unique email address of the user passwordhash string hashed password for secure authentication createdat date timestamp when the account was created updatedat date timestamp when the account was updated you can create these collections and fields manually via the back4app dashboard create new class you can add columns by choosing a data type, naming the field, setting default values, and marking them as required create column utilizing the back4app ai agent for schema generation the back4app ai agent simplifies schema setup by automatically generating your data model based on your description this speeds up configuration and ensures your data structures support all crud operations how to use the ai agent open the ai agent access the ai agent from your back4app project settings outline your data schema describe the collections and fields you need review and apply the ai agent will suggest a schema review the proposal and apply the changes example prompt create the following collections in my back4app project 1\) collection items \ fields \ id objectid (auto generated) \ title string \ description string \ createdat date (auto generated) \ updatedat date (auto updated) 2\) collection users \ fields \ id objectid (auto generated) \ username string (unique) \ email string (unique) \ passwordhash string \ createdat date (auto generated) \ updatedat date (auto updated) this method ensures a consistent and optimized data model step 3 – enabling the admin app & performing crud operations overview of the admin app the back4app admin app provides a no code interface for managing your backend data its drag and drop design makes it simple to execute crud tasks like adding, viewing, updating, and deleting records activating the admin app go to the “more” section in your back4app dashboard select “admin app” and click “enable admin app ” configure your admin credentials by setting up your initial admin account this process will also create roles (e g , b4aadminuser ) and system classes enable admin app after activation, log in to the admin app to manage your data admin app dashboard managing crud operations with the admin app inside the admin app you can insert records use the “add record” button within a collection (for example, items) to create new entries review and edit records click on any record to inspect its details or update its fields delete records remove entries that are no longer required this user friendly interface simplifies data management considerably step 4 – linking your node js application with back4app with your backend set up, it’s time to connect your node js app to back4app option a using the parse javascript sdk install the parse javascript sdk run the following command in your project directory npm install parse initialize parse in your node js application create a configuration file (e g , parseconfig js ) // parseconfig js const parse = require('parse/node'); parse initialize('your application id', 'your javascript key'); parse serverurl = 'https //parseapi back4app com'; module exports = parse; 3\ implement crud operations for example, create a service to manage items ```javascript // itemsservice js const parse = require(' /parseconfig'); async function fetchitems() { const items = parse object extend('items'); const query = new parse query(items); try { const results = await query find(); console log('fetched items ', results); return results; } catch (error) { console error('error retrieving items ', error); } } async function createitem(title, description) { const items = parse object extend('items'); const item = new items(); item set('title', title); item set('description', description); try { await item save(); console log('item successfully created '); } catch (error) { console error('error creating item ', error); } } async function updateitem(objectid, newtitle, newdescription) { const items = parse object extend('items'); const query = new parse query(items); try { const item = await query get(objectid); item set('title', newtitle); item set('description', newdescription); await item save(); console log('item updated successfully '); } catch (error) { console error('error updating item ', error); } } async function deleteitem(objectid) { const items = parse object extend('items'); const query = new parse query(items); try { const item = await query get(objectid); await item destroy(); console log('item deleted successfully '); } catch (error) { console error('error deleting item ', error); } } module exports = { fetchitems, createitem, updateitem, deleteitem, }; option b using rest or graphql if you prefer not to use the parse sdk, you can interact with back4app via rest api calls for instance, here is how to retrieve items using node js const https = require('https'); function fetchitemsrest() { const options = { hostname 'parseapi back4app com', path '/classes/items', method 'get', headers { 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key' } }; const req = https request(options, res => { let data = ''; res on('data', chunk => data += chunk); res on('end', () => console log('response ', data)); }); req on('error', error => console error('error ', error)); req end(); } fetchitemsrest(); integrate these rest calls into your node js services as needed step 5 – strengthening your backend security configuring access control lists (acls) enhance your data security by setting acls on your objects for example, to make an item accessible only to its creator const parse = require(' /parseconfig'); async function createprivateitem(title, description, owner) { const items = parse object extend('items'); const item = new items(); item set('title', title); item set('description', description); const acl = new parse acl(); acl setreadaccess(owner, true); acl setwriteaccess(owner, true); acl setpublicreadaccess(false); acl setpublicwriteaccess(false); item setacl(acl); try { await item save(); console log('private item created successfully '); } catch (error) { console error('error saving private item ', error); } } setting class level permissions (clps) adjust clps in your back4app dashboard to enforce default security rules, ensuring that only authenticated users can interact with specific collections step 6 – implementing user authentication configuring user management back4app leverages parse’s built in user collection to manage authentication in your node js app, implement user sign up and login as shown below const parse = require(' /parseconfig'); async function signup(username, password, email) { const user = new parse user(); user set('username', username); user set('password', password); user set('email', email); try { await user signup(); console log('user registered successfully!'); } catch (error) { console error('error during sign up ', error); } } async function login(username, password) { try { const user = await parse user login(username, password); console log('user logged in ', user get('username')); } catch (error) { console error('login error ', error); } } module exports = { signup, login }; a similar approach can be used for session management and additional authentication features step 7 – deploying your node js application back4app provides a smooth deployment process you can deploy your node js app using several methods, including docker containerization 7 1 building your application compile and package use your preferred build tool (such as npm or yarn) to prepare your application for example, run npm run build verify your build ensure that your production bundle contains all required modules and files 7 2 organizing your project layout a typical node js project structure might look like basic crud app node/ \| src/ \| | controllers/ \| | | itemscontroller js \| | | authcontroller js \| | models/ \| | | item js \| | | user js \| | routes/ \| | itemsroutes js \| | authroutes js \| parseconfig js \| package json \| readme md 7 3 dockerizing your node js app if you prefer containerized deployment, add a dockerfile to your project root \# use an official node js runtime as a parent image from node 14 alpine \# set the working directory workdir /usr/src/app \# copy package files and install dependencies copy package json / run npm install \# copy the rest of the application code copy \# expose the application port (adjust if necessary) expose 3000 \# start the node js application cmd \["npm", "start"] 7 4 deploying via back4app web deployment connect your github repository host your node js source code on github configure deployment settings in your back4app dashboard, choose the web deployment option, link your repository (e g , basic crud app node ), and select the desired branch set build and output commands define your build command (e g , npm run build ) and specify the output folder deploy your app click deploy and monitor the logs until your application is live step 8 – wrapping up and future enhancements congratulations! you have now built a node js crud application integrated with back4app you configured a project called basic crud app node , established collections for items and users, and managed your backend with the back4app admin app furthermore, you connected your node js app via the parse javascript sdk (or rest/graphql) and implemented strong security measures next steps enhance your application consider adding features such as advanced filtering, detailed views, or real time updates expand backend capabilities explore cloud functions, third party api integrations, or role based access controls deepen your expertise check out the back4app documentation https //www back4app com/docs and additional tutorials to further refine your application happy coding and best wishes on your journey with your node js crud application!