1. Developing a basic Python or NodeJs Application
Build a Python Flask App
1.1. Create a python virtual environment
-
As a best practice create a Virtual Environment to get started. In terminal, run the following command:
pip install virtualenv virtualenv venv -
Once the virtualenv is created, source into the venv by running the following command:
source venv/bin/activate -
You will see the prompt change to
(venv)$.
1.2. Install flask library
- Since we are building a flask app make sure to install the flask library, by running the following command:
pip install flask
1.3. Create a python app
-
Create an
app.pyfile by running the following command:touch app.py -
Open the
app.pyin your favourite code editor.-
Import the flask module
from flask import Flask -
Set app context
app = Flask(__name__) -
Define a route for
/@app.route('/') def index(): return "<h1 align='center'>Hello World from Python Flask!</h1>" -
Finally set the flask run
if __name__ == "__main__": app.run(host='0.0.0.0', port=8080, debug=True, use_reloader=True)
-
-
Your
app.pyfile should look something like this:app.py1 2 3 4 5 6 7 8 9 10
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "<h1 align='center'>Hello World from Python Flask!</h1>" if __name__ == "__main__": app.run(host='0.0.0.0', port=8080, debug=True, use_reloader=True) -
Save the file.
1.4. Run the App
-
Back to terminal, run the following command to start the application and check whether it works
python app.py -
You will see output similar to the following:
* Serving Flask app "app" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 812*299*939 -
Visit
http://0.0.0.0:8080/on your browser or run the following command:curl http://0.0.0.0:8080 -
You will see output similar to the following:
<h1 align='center'>Hello World from Python Flask!</h1> -
If you see
Hello Worldmessage, congrats you have successfully built the basic flask application.
Build a NodeJs Express App
1.1. Initialize Node Environment
- Initialize a new project by running the following command:
npm init
1.2. Install Express library
-
Since we are building an Express app make sure to install the Express library by running the following command:
npm install express -
A
package.jsonfile will be created in the root directory of your project.
1.3. Set npm start command
- Add
app.jsto thepackage.jsonscripts section:"scripts": { "start": "node app.js", "test": "echo \"Error: no test specified\" && exit 1" },
1.4. Create a NodeJs Express app
-
Create an
app.jsfile by running the following command:touch app.js -
Open the
app.jsin your favourite code editor.-
Import the express module
const express = require('express'); -
Set app context
const app = express(); -
Set port on which you want the app to listen
const port = 3000; -
Define a route for
/app.get('/', (req, res) => { res.send("<h1 align='center'>Hello World from NodeJs Express!</h1>"); }); -
Finally set the express run
app.listen(port, () => { console.log(`Example app listening on port ${port}!`); });
-
-
Your
app.jsfile should look something like this:app.js1 2 3 4 5 6 7 8 9 10 11
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send("<h1 align='center'>Hello World from NodeJs Express!</h1>"); }); app.listen(port, () => { console.log(`Example app listening on port ${port}!`); }); -
Save the file.
1.5. Run the App
-
Back to terminal, run the following command to start the application and check whether it works
npm start -
You will see output similar to the following:
Example app listening on port 3000! -
Visit
http://0.0.0.0:3000/on your browser or run the following command:curl http://0.0.0.0:3000 -
You will see output similar to the following:
<h1 align='center'>Hello World from Python Flask!</h1> -
If you see
Hello Worldmessage, congrats you have successfully built the basic express application.