DEV Community

Cover image for First Steps with Express.js (Node.js) : Starter Setup, Learn Basics for Beginners | Part 2
koshirok096
koshirok096

Posted on

First Steps with Express.js (Node.js) : Starter Setup, Learn Basics for Beginners | Part 2

Intoroduction

Until Part 1, you created your own Express server! In this article, I’d dig a little deeper Express to learn few more basics.

But to be honest, what I would explain this time are very simple and easy things, so relax, and enjoy your coding practice :)

Let's start with app.get()!

App.get()

app.get(path, callback) is a function used to associate an HTTP GET request with path. When client send a GET request to the path that you set (first argument), callback function (second argument) would be excuted.

Here is an example code:


import express from "express";

const app = express();

// just added!
app.get("/", (req, res) => {
    res.send("response come from server to path");
})

app.listen(8000, () => {
    console.log("Hey! I listen to port 8000");
})

Enter fullscreen mode Exit fullscreen mode

In this code, I set the root path "/" to the first argment and (req, res) for second. '(req, res) => {}' is a handler function, which can be used to retrieve request and response objects.

This "req" and "res" are request and response objects - literaly, "req" is about request from client, and "res" is about response from server.

In this example code, syntax returns my message by using 'send' method, when client send a GET request to the path.

The logic is simple as you see - now, you are able to send a request when client access to spefic path on your Express server!

App.post()

app.post(path, callback) is also a basic, but important function that you should know - It can be used to create form submissions, etc.

Function used to associate an HTTP POST request with path. When client send a POST request to the path that you set (first argument), callback function (second argument) would be excuted. The code structure is similer to above app.get(), but usage is different. Let's see the example code.


import express from "express";

const app = express();

// form submissions example
app.post("/submit", (req, res) => {
  const formData = req.body;
  // Process the form data, something code here ...
  res.send("Form submitted");
});

// app.get("/", (req, res) => {
//     res.send("response come from server to path");
// })

app.listen(8000, () => {
    console.log("Hey! I listen to port 8000");

Enter fullscreen mode Exit fullscreen mode

This is a simple form submission example.

When an user submits a form on a web page, the common way is that the data to sent as a HTTP POST request to the server. Your app.post() method handles the form submission in order to retrieve the form data, from the client request (req.body) and accordingly process it.

In this example, provided code is not completed yet. But you can add some code inside of app.post() depending on the design or purpose of the project to complete!

Conclusion

I share just only basic methods in this article, but there some good resources in here Dev.to or Google. If you interested in learning more, let's do it! And I hope it helps your learning :)
See you soon :D

Top comments (0)