Serve data to clients!
How does this process work?
Suppose someone types Wikipedia.org into their browser. Their browser (the client), then sends a ‘request’ to the server. The server handles that ‘request’ and returns data in the form of a ‘response’ to the client. That response ends up being html with links for static resources such as css, javascript, and assets (i.e. images), to be presented to the client. In other words, the webpage!
Build a web app enabling visitors to subscribe to a Mailchimp newsletter.
TOOLS: NodeJS, npm, Express, and the Mailchimp api.
Deployed Web App: https://newsletter-signup-009x.onrender.com/
Repository Template (no API keys): https://github.com/michaelpgalen/newsletter-signup-template
(assumes node.js and npm are installed)
First, import and setup the ‘express’ package. It helps make our code faster to write and easier to read.
“Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.”
Setup the server to listen on our localhost port 3000 for development
app.listen(3000, console.log("Server running on PORT 3000")
Handle GET requests at the directory root route (app.js)
app.get("/", function(req, res) {
res.sendFile(__dirname + "/signup.html")})
Below is the code for our server handling POST requests at the root route, and making its own request to Mailchimp before sending a response.
Render.com takes the place Heroku filled for a while, offering free hosting for small hobby web apps, and providing paid services when you need to scale. As described on Render’s website:
“Render is a unified cloud to build and run all your apps and websites with free TLS certificates, a global CDN, DDoS protection, private networks, and auto deploys from Git.” — Render.com
Plus, Render is setup with NodeJS.
Your web service should be deployed shortly!