
Glitch is one of free platform, that can help you make your application online. Glitch give us 1000 hour per month, more than we need more for deploy one application.
Start coding
For this tutorial, we will make a simple counter application.
All required module:
- Filesystem (fs)
- Web server (http)
Note: Make sure you have installenfs
module. If you don't have it installed, you can install by run this command:
npm install fs
Make a file namedindex.js
, and fill with this code:
// Adding required moduleconst http = require('http');const fs = require('fs');// For development, use 4040const port = 4040// Set limitconst limit = 10// Run serverserver = http.createServer(function (req, res) { fs.readFile('counter.txt', 'utf8', function (err,data) { if (err) { return console.log(err); } // Check if page view(s) is less than limit if (limit > parseInt(data)) { // If the page view(s) is less than limit res.end(`Hey, this page view(s) is ${data}! Can this page reach ${limit} views?`); } else { // If the page view(s) is more or same than limit res.end(`Hey, this page view(s) is ${data}! Yay, the page views is reach ${limit}!`); } // Add +1 to the counter file fs.writeFile('counter.txt', parseInt(data) + 1, 'utf8', function (err) { if (err) return console.log(err); }); });}).listen(port)
Create also the file that namedcounter.txt
. You can fill it with0
, or any number that you like.
Finnaly, let's run our app. Type this command:
node index.js
Then, fire up your browser (you have fire up it, LOL), then openhttp://localhost:4040.
I try refresh that for ten times. The text will change.
Coding, check.
Moving to Glitch
At first, you need to register and create an account at Glitch, after that, open your dashboard and create a new project (selectglitch-hello-node
option). You can delete all the files, exceptpackage.json
and.env
. It's ok if you wan't to keep the file to.
After that, back to the editor, we need to modify the port, so our script can work with Glitch.
Modifyport
variable value (line 7) to this:process.env.PORT
So, the 7th line will look like this:const port = process.env.PORT
Save the changes, upload theindex.js
andcounter.txt
to your Glitch project. The file structure will looks like this:
We need to edit thepackage.json
file. Maybe this is the hardest part from this tutorial. So, be careful. At first, we need to change the script, with ourindex.js
file. Replace"start": "node server.js"
with"start": "node index.js"
. After that, we need to add some module that we need. You can add it by simply click "Add package" button
After clicking, a form will appear. Click the input with "search npm for packages" text, and type "express".
Click the first result. Do the same way, and add thefs
package.
Select the second one, and we done. You can see the result by press the refresh button (the second button from left).
That is our project today. Hope you enjoy the project. Bye!
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse