From the course: HTMX Essential Training
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Creating a webserver - HTMX Tutorial
From the course: HTMX Essential Training
Creating a webserver
- [Instructor] In order to post some data to a web server, we need to create a small REST API to make that work, so we're going to create a new file here called server.js, and we'll use a few features of the NodeJS standard library to make this work. So we'll say http will require http. Then we will set a port of some sort. (keyboard clacking) We then will create our server. The http.createserver function is going to take in a callback function. This callback function will say request, response. We'll say, if the request.method equals POST, then we want to create a value called body. This is just going to be an empty string into which we'll pump in some new JSON. So for now, it's empty, but when the request happens, we're going to create a listener for that. So when there's some new data, we want to take that chunk, and we want to append to that chunk to the body. So we'll say plus equals chunk. And then we'll say request.on. So when the end event fires, in other words, when we're out…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.