On this tutorial, I’ll reveal easy methods to construct a totally functioning CRUD app utilizing Node for the backend and htmx for the frontend. This can reveal how htmx integrates right into a full-stack utility, permitting you to evaluate its effectiveness and resolve if it’s a good selection on your future initiatives.
htmx is a contemporary JavaScript library designed to reinforce internet functions by enabling partial HTML updates with out the necessity for full web page reloads. It does this by sending HTML over the wire, versus the JSON payload related to conventional frontend frameworks.
What We’ll Be Constructing
We’ll develop a easy contact supervisor able to all CRUD actions: creating, studying, updating, and deleting contacts. By leveraging htmx, the appliance will supply a single-page utility (SPA) really feel, enhancing interactivity and consumer expertise.
If customers have JavaScript disabled, the app will work with full-page refreshes, sustaining usability and discoverability. This method showcases htmx’s skill to create trendy internet apps whereas preserving them accessible and Search engine marketing-friendly.
Right here’s what we’ll find yourself with.
The code for this text might be discovered on the accompanying GitHub repo.
Stipulations
To comply with together with this tutorial, you’ll want Node.js put in in your PC. For those who don’t have Node put in, please head to the official Node obtain web page and seize the proper binaries on your system. Alternatively, you would possibly wish to set up Node utilizing a model supervisor. This method permits you to set up a number of Node variations and change between them at will.
Aside from that, some familiarity with Node, Pug (which we’ll be utilizing because the template engine) and htmx can be useful, however not important. For those who’d like a refresher on any of the above, take a look at our tutorials: Construct a Easy Newbie App with Node, A Information to the Pug HTML Template Preprocessor and An Introduction to htmx.
Earlier than we start, run the next instructions:
node -v
npm -v
You need to see output like this:
v20.11.1
10.4.0
This confirms that Node and npm are put in in your machine and are accessible out of your command line atmosphere.
Setting Up the Undertaking
Let’s begin by scaffolding a brand new Node venture:
mkdir contact-manager
cd contact-manager
npm init -y
This could create a bundle.json
file within the venture root.
Subsequent, let’s set up the dependencies we’re going to want:
npm i categorical method-override pug
Of those packages, Categorical is the spine of our app. It’s a quick and minimalist internet framework which presents a simple option to deal with requests and responses, and to route URLs to particular handler features. Pug will function our template engine, whereas we’ll use method-override to make use of HTTP verbs like PUT and DELETE in locations the place the shopper doesn’t assist them.
Subsequent, create an app.js
file within the root listing:
contact app.js
And add the next content material:
const categorical = require('categorical');
const path = require('path');
const routes = require('./routes/index');
const app = categorical();
app.set('views', path.be a part of(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(categorical.static('public'));
app.use("https://www.sitepoint.com/", routes);
const server = app.pay attention(3000, () => {
console.log(`Categorical is working on port ${server.tackle().port}`);
});
Right here, we’re organising the construction of our Categorical app. This contains configuring Pug as our view engine for rendering views, defining the listing for our static belongings, and hooking up our router.
The appliance listens on port 3000, with a console log to substantiate that Categorical is working and able to deal with requests on the desired port. This setup varieties the bottom of our utility and is able to be prolonged with additional performance and routes.
Subsequent, let’s create our routes file:
mkdir routes
contact routes/index.js
Open that file and add the next:
const categorical = require('categorical');
const router = categorical.Router();
router.get('/contacts', async (req, res) => {
res.ship('It really works!');
});
Right here, we’re organising a primary route inside our newly created routes listing. This route listens for GET requests on the /contacts
endpoint and responds with a easy affirmation message, indicating that every little thing is functioning correctly.
Subsequent, replace the “scripts” part of the bundle.json
file with the next:
"scripts": {
"dev": "node --watch app.js"
},
This makes use of the brand new watch mode in Node.js, which is able to restart our app at any time when any adjustments are detected.
Lastly, boot every little thing up with npm run dev
and head to http://localhost:3000/contacts/ in your browser. You need to see a message saying “It really works!”.
Thrilling occasions!
Now let’s add some contacts to show. As we’re specializing in htmx, we’ll use a hard-coded array for simplicity. This can preserve issues streamlined, permitting us to concentrate on htmx’s dynamic options with out the complexity of database integration.
For these eager about including a database afterward, SQLite and Sequelize are nice decisions, providing a file-based system that doesn’t require a separate database server.
With that mentioned, please add the next to index.js
earlier than the primary route:
const contacts = [
{ id: 1, name: 'John Doe', email: 'john.doe@example.com' },
{ id: 2, name: 'Jane Smith', email: 'jane.smith@example.com' },
{ id: 3, name: 'Emily Johnson', email: 'emily.johnson@example.com' },
{ id: 4, name: 'Aarav Patel', email: 'aarav.patel@example.com' },
{ id: 5, name: 'Liu Wei', email: 'liu.wei@example.com' },
{ id: 6, name: 'Fatima Zahra', email: 'fatima.zahra@example.com' },
{ id: 7, name: 'Carlos Hernández', email: 'carlos.hernandez@example.com' },
{ id: 8, name: 'Olivia Kim', email: 'olivia.kim@example.com' },
{ id: 9, name: 'Kwame Nkrumah', email: 'kwame.nkrumah@example.com' },
{ id: 10, name: 'Chen Yu', email: 'chen.yu@example.com' },
];
Now we have to create a template for our path to show. Create a views
folder containing an index.pug
file:
mkdir views
contact views/index.pug
And add the next:
doctype html
html
head
meta(charset='UTF-8')
title Contact Supervisor
hyperlink(rel='preconnect', href='https://fonts.googleapis.com')
hyperlink(rel='preconnect', href='https://fonts.gstatic.com', crossorigin)
hyperlink(href='https://fonts.googleapis.com/css2?household=Roboto:wght@300;400&show=swap', rel='stylesheet')
hyperlink(rel='stylesheet', href='/types.css')
physique
header
a(href='/contacts')
h1 Contact Supervisor
part#sidebar
ul.contact-list
every contact in contacts
li #{contact.title}
div.actions
a(href='/contacts/new') New Contact
important#content material
p Choose a contact
script(src='https://unpkg.com/htmx.org@1.9.10')
On this template, we’re laying out the HTML construction for our utility. Within the head part, we’re together with the Roboto font from Google Fonts and a stylesheet for customized types.
The physique is split right into a header, a sidebar for itemizing contacts, and a important content material space the place all of our contact data will go. The content material space at present accommodates a placeholder. On the finish of the physique we’re additionally together with the most recent model of the htmx library from a CDN.
The template expects to obtain an array of contacts (in a contacts
variable), which we iterate over within the sidebar and output every contact title in an unordered record utilizing Pug’s interpolation syntax.
Subsequent, let’s create the customized stylesheet:
mkdir public
contact public/types.css
I don’t intend to record the types right here. Please copy them from the CSS file within the accompanying GitHub repo, or be happy so as to add a few of your individual. 🙂
Again in index.js
, let’s replace our route to make use of the template:
router.get('/contacts', (req, res) => {
res.render('index', { contacts });
});
Now once you refresh the web page you must see one thing like this.
To date, all we’ve finished is about up a primary Categorical app. Let’s change that and at last add htmx to the combination. The following step is to make it in order that when a consumer clicks on a contact within the sidebar, that contact’s data is displayed in the principle content material space — naturally with out a full web page reload.
To begin with, let’s transfer the sidebar into its personal template:
contact views/sidebar.pug
Add the next to this new file:
ul.contact-list
every contact in contacts
li
a(
href=`/contacts/${contact.id}`,
hx-get=`/contacts/${contact.id}`,
hx-target='#content material',
hx-push-url='true'
)= contact.title
div.actions
a(href='/contacts/new') New Contact
Right here we have now made every contact a hyperlink pointing to /contacts/${contact.id}
and added three htmx attributes:
hx-get
. When the consumer clicks a hyperlink, htmx will intercept the press and make a GET request through Ajax to the/contacts/${contact.id}
endpoint.hx-target
. When the request completes, the response can be inserted into the div with an ID ofcontent material
. We haven’t specified any sort of swap technique right here, so the contents of the div can be changed with no matter is returned from the Ajax request. That is the default conduct.hx-push-url
. This can make sure that the worth laid out inhtx-get
is pushed onto the browser’s historical past stack, altering the URL.
Replace index.pug
to make use of our template:
part#sidebar
embody sidebar.pug
Bear in mind: Pug is white house delicate, so you should definitely use the proper indentation.
Now let’s create a brand new endpoint in index.js
to return the HTML response that htmx is anticipating:
router.get('/contacts/:id', (req, res) => {
const { id } = req.params;
const contact = contacts.discover((c) => c.id === Quantity(id));
res.ship(`
<h2>${contact.title}</h2>
<p><sturdy>Title:</sturdy> ${contact.title}</p>
<p><sturdy>Electronic mail:</sturdy> ${contact.e-mail}</p>
`);
});
For those who save this and refresh your browser, you must now have the ability to view the small print of every contact.
HTML over the wire
Let’s take a second to grasp what’s occurring right here. As talked about originally of the article, htmx delivers HTML over the wire, versus the JSON payload related to conventional frontend frameworks.
We are able to see this if we open our browser’s developer instruments, change to the Community tab and click on on one of many contacts. Upon receiving a request from the frontend, our Categorical app generates the HTML wanted to show that contact and sends it to the browser, the place htmx swaps it into the proper place within the UI.
So issues are going fairly nicely, huh? Because of htmx, we simply made our web page dynamic by specifying a few attributes on an anchor tag. Sadly, there’s an issue…
For those who show a contact, then refresh the web page, our beautiful UI is gone and all you see is the naked contact particulars. The identical will occur for those who load the URL straight in your browser.
The explanation for that is apparent if you concentrate on it. If you entry a URL similar to http://localhost:3000/contacts/1, the Categorical route for '/contacts/:id'
kicks in and returns the HTML for the contact, as we’ve instructed it to do. It is aware of nothing about the remainder of our UI.
To fight this, we have to make a few adjustments. On the server, we have to examine for an HX-Request header, which signifies that the request got here from htmx. If this header exists, then we will ship our partial. In any other case, we have to ship the total web page.
Change the route handler like so:
router.get('/contacts/:id', (req, res) => {
const { id } = req.params;
const contact = contacts.discover((c) => c.id === Quantity(id));
if (req.headers['hx-request']) {
res.ship(`
<h2>${contact.title}</h2>
<p><sturdy>Title:</sturdy> ${contact.title}</p>
<p><sturdy>Electronic mail:</sturdy> ${contact.e-mail}</p>
`);
} else {
res.render('index', { contacts });
}
});
Now, once you reload the web page, the UI doesn’t disappear. It does, nonetheless, revert from whichever contact you had been viewing to the message “Choose a contact”, which isn’t very best.
To repair this, we will introduce a case
assertion to our index.pug
template:
important#content material
case motion
when 'present'
h2 #{contact.title}
p #[strong Name:] #{contact.title}
p #[strong Email:] #{contact.e-mail}
when 'new'
when 'edit'
default
p Choose a contact
And at last replace the route handler:
if (req.headers['hx-request']) {
} else {
res.render('index', { motion: 'present', contacts, contact });
}
Be aware that we’re now passing in a contact
variable, which can be used within the occasion of a full web page reload.
And with this, our app ought to face up to being refreshed or having a contact loaded straight.
A fast refactor
Though this works, you would possibly discover that we have now some duplicate content material in each our route handler and our important pug template. This isn’t very best, and issues will begin to get unwieldy as quickly as a contact has something greater than a handful of attributes, or we have to use some logic to resolve which attributes to show.
To counteract this, let’s transfer contact into its personal template:
contact views/contact.pug
Within the newly created template, add this:
h2 #{contact.title}
p #[strong Name:] #{contact.title}
p #[strong Email:] #{contact.e-mail}
In the principle template (index.pug
):
important#content material
case motion
when 'present'
embody contact.pug
And our route handler:
if (req.headers['hx-request']) {
res.render('contact', { contact });
} else {
res.render('index', { motion: 'present', contacts, contact });
}
Issues ought to nonetheless work as earlier than, however now we’ve eliminated the duplicated code.
The following job to show our consideration to is creating a brand new contact. This a part of the tutorial will information you thru organising the shape and backend logic, utilizing htmx to deal with submissions dynamically.
Let’s begin by updating our sidebar template. Change:
div.actions
a(href='/contacts/new') New Contact
… to:
div.actions
a(
href='/contacts/new',
hx-get='/contacts/new',
hx-target='#content material',
hx-push-url='true'
) New Contact
This makes use of the identical htmx attributes as our hyperlinks to show a contact: hx-get
will make a GET request through Ajax to the /contacts/new
endpoint, hx-target
specifies the place to insert the response, and hx-push-url
will make sure that the URL is modified.
Now let’s create a brand new template for the shape:
contact views/kind.pug
And add the next code:
h2 New Contact
kind(
motion='/contacts',
methodology='POST',
hx-post='/contacts',
hx-target='#sidebar',
hx-on::after-request='if(occasion.element.profitable) this.reset()'
)
label(for='title') Title:
enter#title(sort='textual content', title='title', required)
label(for='e-mail') Electronic mail:
enter#e-mail(sort='e-mail', title='e-mail', required)
div.actions
button(sort='submit') Submit
Right here, we’re utilizing the hx-post
attribute to inform htmx to intercept the shape submission and make a POST request with the shape information to the /contacts
endpoint. The consequence (an up to date record of contacts) can be inserted into the sidebar. We don’t wish to change the URL on this case, because the consumer would possibly wish to enter a number of new contacts. We do, nonetheless, wish to empty the shape after a profitable submission, which is what the hx-on::after-request
does. The hx-on*
attributes mean you can embed scripts inline to reply to occasions straight on a component. You possibly can learn extra about it right here.
Subsequent, let’s add a route for the shape in index.js
:
...
router.get('/contacts/new', (req, res) => {
if (req.headers['hx-request']) {
res.render('kind');
} else {
res.render('index', { motion: 'new', contacts, contact: {} });
}
});
...
Route order is necessary right here. When you’ve got the '/contacts/:id'
route first, then Categorical will try to discover a contact with the ID of new
.
Lastly, replace our index.pug
template to make use of the shape:
when 'new'
embody kind.pug
Refresh the web page, and at this level you must have the ability to render the brand new contact kind by clicking on the New Contact hyperlink within the sidebar.
Now we have to create a path to deal with kind submission.
First replace app.js
to present us entry to the shape’s information inside our route handler.
const categorical = require('categorical');
const path = require('path');
const routes = require('./routes/index');
const app = categorical();
app.set('views', path.be a part of(__dirname, 'views'));
app.set('view engine', 'pug');
+ app.use(categorical.urlencoded({ prolonged: true }));
app.use(categorical.static('public'));
app.use("https://www.sitepoint.com/", routes);
const server = app.pay attention(3000, () => {
console.log(`Categorical is working on port ${server.tackle().port}`);
});
Beforehand, we’d have used the body-parser bundle, however I lately discovered that is not mandatory.
Then add the next to index.js
:
router.put up('/contacts', (req, res) => {
const newContact = {
id: contacts.size + 1,
title: req.physique.title,
e-mail: req.physique.e-mail,
};
contacts.push(newContact);
if (req.headers['hx-request']) {
res.render('sidebar', { contacts });
} else {
res.render('index', { motion: 'new', contacts, contact: {} });
}
});
Right here, we’re creating a brand new contact with the info we acquired from the shopper and including it to the contacts
array. We’re then re-rendering the sidebar, passing it the up to date record of contacts.
Be aware that, for those who’re making any sort of utility that has customers, it’s as much as you to validate the info you’re receiving from the shopper. In our instance, I’ve added some primary client-side validation, however this could simply be bypassed.
There’s an instance of easy methods to validate enter on the server utilizing the express-validator bundle bundle within the Node tutorial I linked to above.
Now, for those who refresh your browser and take a look at including a contact, it ought to work as anticipated: the brand new contact ought to be added to the sidebar and the shape ought to be reset.
Including flash messages
That is nicely and good, however now we want a option to inform the consumer {that a} contact has been added. In a typical utility, we’d use a flash message — a brief notification that alerts the consumer in regards to the end result of an motion.
The issue we encounter with htmx is that we’re updating the sidebar after efficiently creating a brand new contact, however this isn’t the place we wish our flash message to be displayed. A greater location can be above the brand new contact kind.
To get round this, we will use the hx-swap-oob
attribute. This lets you specify that some content material in a response ought to be swapped into the DOM someplace apart from the goal, that’s “Out of Band”.
Replace the route handler as follows:
if (req.headers['hx-request']) {
res.render('sidebar', { contacts }, (err, sidebarHtml) => {
const html = `
<important id="content material" hx-swap-oob="afterbegin">
<p class="flash">Contact was efficiently added!</p>
</important>
${sidebarHtml}
`;
res.ship(html);
});
} else {
res.render('index', { motion: 'new', contacts, contact: {} });
}
Right here, we’re rendering the sidebar as earlier than, however passing the render
methodology an nameless perform because the third parameter. This perform receives the HTML generated by calling res.render('sidebar', { contacts })
, which we will then use to assemble our remaining response.
By specifying a swap technique of "afterbegin"
, the flash message is inserted on the prime of the container.
Now, after we add a contact, we must always get a pleasant message informing us what occurred.
For updating a contact, we’re going to reuse the shape we created within the earlier part.
Let’s begin by updating our contact.pug
template so as to add the next:
div.actions
a(
href=`/contacts/${contact.id}/edit`,
hx-get=`/contacts/${contact.id}/edit`,
hx-target='#content material',
hx-push-url='true'
) Edit Contact
This can add an Edit Contact button beneath a contacts particulars. As we’ve seen earlier than, when the hyperlink is clicked, hx-get
will make a GET request through Ajax to the /${contact.id}/edit
endpoint, hx-target
will specify the place to insert the response, and hx-push-url
will make sure that the URL is modified.
Now let’s alter our index.pug
template to make use of the shape:
when 'edit'
embody kind.pug
Additionally add a route handler to show the shape:
router.get('/contacts/:id/edit', (req, res) => {
const { id } = req.params;
const contact = contacts.discover((c) => c.id === Quantity(id));
if (req.headers['hx-request']) {
res.render('kind', { contact });
} else {
res.render('index', { motion: 'edit', contacts, contact });
}
});
Be aware that we’re retrieving the contact utilizing the ID from the request, then passing that contact to the shape.
We’ll additionally have to replace our new contact handler to do the identical, however right here passing an empty object:
// GET /contacts/new
router.get('/contacts/new', (req, res) => {
if (req.headers['hx-request']) {
- res.render('kind');
+ res.render('kind', { contact: {} });
} else {
res.render('index', { motion: 'new', contacts, contact: {} });
}
});
Then we have to replace the shape itself:
- isEditing = () => !(Object.keys(contact).size === 0);
h2=isEditing() ? "Edit Contact" : "New Contact"
kind(
motion=isEditing() ? `/replace/${contact.id}?_method=PUT` : '/contacts',
methodology='POST',
hx-post=isEditing() ? false : '/contacts',
hx-put=isEditing() ? `/replace/${contact.id}` : false,
hx-target='#sidebar',
hx-push-url=isEditing() ? `/contacts/${contact.id}` : false
hx-on::after-request='if(occasion.element.profitable) this.reset()',
)
label(for='title') Title:
enter#title(sort='textual content', title='title', required, worth=contact.title)
label(for='e-mail') Electronic mail:
enter#e-mail(sort='e-mail', title='e-mail', required, worth=contact.e-mail)
div.actions
button(sort='submit') Submit
As we’re passing in both a contact or an empty object to this way, we now have a simple option to decide if we’re in “edit” or “create” mode. We are able to do that by checking Object.keys(contact).size
. We are able to additionally extract this examine into just a little helper perform on the prime of the file utilizing Pug’s unbuffered code syntax.
As soon as we all know which mode we discover ourselves in, we will conditionally change the web page title, then resolve which attributes we add to the shape tag. For the edit kind, we have to add a hx-put
attribute and set it to /replace/${contact.id}
. We additionally have to replace the URL as soon as the contact’s particulars have been saved.
To do all of this, we will make the most of the truth that, if a conditional returns false
, Pug will omit the attribute from the tag.
Which means that this:
kind(
motion=isEditing() ? `/replace/${contact.id}?_method=PUT` : '/contacts',
methodology='POST',
hx-post=isEditing() ? false : '/contacts',
hx-put=isEditing() ? `/replace/${contact.id}` : false,
hx-target='#sidebar',
hx-on::after-request='if(occasion.element.profitable) this.reset()',
hx-push-url=isEditing() ? `/contacts/${contact.id}` : false
)
… will compile to the next when isEditing()
returns false
:
<kind
motion="/contacts"
methodology="POST"
hx-post="/contacts"
hx-target="#sidebar"
hx-on::after-request="if(occasion.element.profitable) this.reset()"
>
...
</kind>
However when isEditing()
returns true
, it would compile to:
<kind
motion="/replace/1?_method=PUT"
methodology="POST"
hx-put="/replace/1"
hx-target="#sidebar"
hx-on::after-request="if(occasion.element.profitable) this.reset()"
hx-push-url="/contacts/1"
>
...
</kind>
In its replace state, discover that the shape motion is "/replace/1?_method=PUT"
. This question string parameter has been added as a result of we’re utilizing the method-override bundle, and it’ll make our router reply to a PUT request.
Out of the field, htmx can ship PUT and DELETE requests, however the browser can’t. Which means, if we wish to take care of a situation the place JavaScript is disabled, we would want to duplicate our route handler, having it reply to each PUT (htmx) and POST (the browser). Utilizing this middleware will preserve our code DRY.
Let’s go forward and add it to app.js
:
const categorical = require('categorical');
const path = require('path');
+ const methodOverride = require('method-override');
const routes = require('./routes/index');
const app = categorical();
app.set('views', path.be a part of(__dirname, 'views'));
app.set('view engine', 'pug');
+ app.use(methodOverride('_method'));
app.use(categorical.urlencoded({ prolonged: true }));
app.use(categorical.static('public'));
app.use("https://www.sitepoint.com/", routes);
const server = app.pay attention(3000, () => {
console.log(`Categorical is working on port ${server.tackle().port}`);
});
Lastly, let’s replace index.js
with a brand new route handler:
router.put('/replace/:id', (req, res) => {
const { id } = req.params;
const newContact = {
id: Quantity(id),
title: req.physique.title,
e-mail: req.physique.e-mail,
};
const index = contacts.findIndex((c) => c.id === Quantity(id));
if (index !== -1) contacts[index] = newContact;
if (req.headers['hx-request']) {
res.render('sidebar', { contacts }, (err, sidebarHtml) => {
res.render('contact', { contact: contacts[index] }, (err, contactHTML) => {
const html = `
${sidebarHtml}
<important id="content material" hx-swap-oob="true">
<p class="flash">Contact was efficiently up to date!</p>
${contactHTML}
</important>
`;
res.ship(html);
});
});
} else {
res.redirect(`/contacts/${index + 1}`);
}
});
Hopefully there’s nothing too mysterious right here by now. Firstly of the handler we seize the contact ID from the request params. We then discover the contact we want to replace and swap it out with a brand new contact created from the shape information we acquired.
When coping with an htmx request, we first render the sidebar template with our up to date contacts record. We then render the contact template with the up to date contact and use the results of each of those calls to assemble our response. As earlier than, we use an “Out of Band” replace to create a flash message informing the consumer that the contact was up to date.
At this level, you must have the ability to replace contacts.
The ultimate piece of the puzzle is the flexibility to delete contacts. Let’s add a button to do that to our contact template:
div.actions
kind(methodology='POST', motion=`/delete/${contact.id}?_method=DELETE`)
button(
sort='submit',
hx-delete=`/delete/${contact.id}`,
hx-target='#sidebar',
hx-push-url='/contacts'
class='hyperlink'
) Delete Contact
a(
)
Be aware that it’s good observe to make use of a kind and a button to challenge the DELETE request. Kinds are designed for actions that trigger adjustments, like deletions, and this ensures semantic correctness. Moreover, utilizing a hyperlink for a delete motion might be dangerous as a result of engines like google can inadvertently comply with hyperlinks, probably resulting in undesirable deletions.
That being mentioned, I’ve added some CSS to model the button like a hyperlink, as buttons are ugly. For those who copied the types from the repo earlier than, you have already got this in your code.
And at last, our route handler in index.js
:
router.delete('/delete/:id', (req, res) => {
const { id } = req.params;
const index = contacts.findIndex((c) => c.id === Quantity(id));
if (index !== -1) contacts.splice(index, 1);
if (req.headers['hx-request']) {
res.render('sidebar', { contacts }, (err, sidebarHtml) => {
const html = `
<important id="content material" hx-swap-oob="true">
<p class="flash">Contact was efficiently deleted!</p>
</important>
${sidebarHtml}
`;
res.ship(html);
});
} else {
res.redirect('/contacts');
}
});
As soon as the contact has been eliminated, we’re updating the sidebar and displaying the consumer a flash message.
Taking It Additional
And that’s a wrap.
On this article, we’ve crafted a full-stack CRUD utility utilizing Node and Categorical for the backend and htmx for the frontend. Alongside the best way, I’ve demonstrated how htmx can simplify including dynamic conduct to your internet apps, decreasing the necessity for complicated JavaScript and full-page reloads, and thus making the consumer expertise smoother and extra interactive.
And as an added bonus, the app additionally features nicely with out JavaScript.
But whereas our app is totally useful, it’s admittedly just a little bare-bones. For those who want to proceed exploring htmx, you would possibly like to take a look at implementing view transitions between app states, or including some additional validation to the shape — for instance, to confirm that the e-mail tackle comes from a selected area.
I’ve examples of each of this stuff (and extra apart from) in my Introduction to htmx.
Aside from that, if in case you have any questions or feedback, please attain out on X.
Pleased coding!