Cara menggunakan line reader nodejs

For a modern web developer, knowing how to work with APIs to facilitate communication between software systems is paramount. In this tutorial, we’ll learn how to create a CRUD RESTful API in a Node.js environment that runs on an Express server and uses a PostgreSQL database. We’ll also walk through connecting an Express server with PostgreSQL using node-postgres.

Our API will be able to handle the HTTP request methods that correspond to the PostgreSQL database from which the API gets its data. You’ll also learn how to install PostgreSQL and work with it through the command-line interface.

Our goal is to allow CRUD operations,

postgres=#
6,
postgres=#
7,
postgres=#
8, and
postgres=#
9, on the API, which will run the corresponding database commands. To do so, we’ll set up a route for each endpoint and a function for each query.

We’ll cover the following in detail:

To follow along with this tutorial, you‘ll need:

  • Familiarity with the JavaScript syntax and fundamentals
  • Basic knowledge of working with the command line
  • Node.js and npm installed

The complete code for the tutorial is available on this GitHub repo. Let’s get started!

What is a RESTful API?

Representational State Transfer (REST) defines a set of standards for web services. An API is an interface that software programs use to communicate with each other. Therefore, a RESTful API is an API that conforms to the REST architectural style and constraints. REST systems are stateless, scalable, cacheable, and have a uniform interface.

What is a CRUD API?

When building an API, you want your model to provide four basic functionalities. It should be able to create, read, update, and delete resources. This set of essential operations is commonly referred to as CRUD.

RESTful APIs most commonly utilize HTTP requests. Four of the most common HTTP methods in a REST environment are

postgres=#
6,
postgres=#
7,
postgres=#
8, and
postgres=#
9, which are the methods by which a developer can create a CRUD system.

  • postgres=# \conninfo
    You are connected to database "postgres" as user "your_username" via socket in "/tmp" at port "5432".
    
    4: Use the
    postgres=# \conninfo
    You are connected to database "postgres" as user "your_username" via socket in "/tmp" at port "5432".
    
    5 method to create a resource in a REST environment
  • postgres=# \conninfo
    You are connected to database "postgres" as user "your_username" via socket in "/tmp" at port "5432".
    
    6: Use the
    postgres=#
    
    6 method to read a resource, retrieving data without altering it
  • postgres=# \conninfo
    You are connected to database "postgres" as user "your_username" via socket in "/tmp" at port "5432".
    
    8: Use the
    postgres=#
    
    8 method to update a resource
  • postgres=# CREATE ROLE me WITH LOGIN PASSWORD 'password';
    
    0: Use the
    postgres=#
    
    9 method to remove a resource from the system

What is Express?

According to the official Express documentation, Express is a fast, un-opinionated, minimalist web framework for Node.js. Express is one of the most popular frameworks for Node.js. In fact, the E in MERN, MEVN, and MEAN stack stands for Express.

Although Express is minimalist, it is also very flexible, leading to the development of various Express middlewares that you can use to address almost any task or problem imaginable.

What is PostgreSQL?

PostgreSQL, commonly referred to as Postgres, is a free, open source relational database management system. You might be familiar with a few other similar database systems, like MySQL, Microsoft SQL Server, or MariaDB, which compete with PostgreSQL.

PostgreSQL is a robust relational database that has been around since 1997 and is available on all major operating systems, Linux, Windows, and macOS. Since PostgreSQL is known for stability, extensibility, and standards compliance, it’s a popular choice for developers and companies.

It’s also possible to create a Node.js RESTful CRUD API using Sequelize. Sequelize is a promise-based Node.js ORM for for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server.

For more on how to use Sequelize in a Node.js REST API, check out the video tutorial below:

What is node-postgres?

node-postgres, or pg, is a nonblocking PostgreSQL client for Node.js. Essentially, node-postgres is a collection of Node.js modules for interfacing with a PostgreSQL database.

Among the many features node-postgres supports include callbacks, promises, async/await, connection pooling, prepared statements, cursors, rich type parsing, and C/C++ bindings.

Creating a PostgreSQL database

We’ll begin this tutorial by installing PostgreSQL, creating a new user, creating a database, and initializing a table with a schema and some data.

Installation

If you’re using Windows, download a Windows installer of PostgreSQL.

If you’re using a Mac, this tutorial assumes you have Homebrew installed on your computer as a package manager for installing new programs. If you don’t, simply click on the link and follow the instructions.

Open up the Terminal and install

postgres=# CREATE ROLE me WITH LOGIN PASSWORD 'password';
2 with
postgres=# CREATE ROLE me WITH LOGIN PASSWORD 'password';
3:

brew install postgresql

You may see instructions on the web reading

postgres=# CREATE ROLE me WITH LOGIN PASSWORD 'password';
4 instead of
postgres=# CREATE ROLE me WITH LOGIN PASSWORD 'password';
2; both options will install PostgreSQL on your computer.


Cara menggunakan line reader nodejs
Cara menggunakan line reader nodejs

Over 200k developers use LogRocket to create better digital experiences

Cara menggunakan line reader nodejs
Cara menggunakan line reader nodejs
Learn more →


After the installation is complete, we’ll want to get

postgres=# CREATE ROLE me WITH LOGIN PASSWORD 'password';
2 up and running, which we can do with
postgres=# CREATE ROLE me WITH LOGIN PASSWORD 'password';
7:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

If at any point you want to stop the

postgres=# CREATE ROLE me WITH LOGIN PASSWORD 'password';
2 service, you can run
postgres=# CREATE ROLE me WITH LOGIN PASSWORD 'password';
9.

With PostgreSQL installed, next, we’ll connect to the

postgres=# ALTER ROLE me CREATEDB;
0 command line where we can run SQL commands.

PostgreSQL command prompt

postgres=# ALTER ROLE me CREATEDB;
1 is the PostgreSQL interactive terminal. Running
postgres=# ALTER ROLE me CREATEDB;
1 will connect you to a PostgreSQL host. Running
postgres=# ALTER ROLE me CREATEDB;
3 will give you more information about the available options for connecting with
postgres=# ALTER ROLE me CREATEDB;
1:

  • postgres=# ALTER ROLE me CREATEDB;
    
    5:
    postgres=# ALTER ROLE me CREATEDB;
    
    6: The database server host or socket directory; the default is
    postgres=# ALTER ROLE me CREATEDB;
    
    7
  • postgres=# ALTER ROLE me CREATEDB;
    
    8:
    postgres=# ALTER ROLE me CREATEDB;
    
    9: The database server port; the default is
    me          | Create DB                           | {}
    postgres    | Superuser, Create role, Create DB   | {}
    
    0
  • me          | Create DB                           | {}
    postgres    | Superuser, Create role, Create DB   | {}
    
    1:
    me          | Create DB                           | {}
    postgres    | Superuser, Create role, Create DB   | {}
    
    2: The database username; the default is
    me          | Create DB                           | {}
    postgres    | Superuser, Create role, Create DB   | {}
    
    3
  • me          | Create DB                           | {}
    postgres    | Superuser, Create role, Create DB   | {}
    
    4:
    me          | Create DB                           | {}
    postgres    | Superuser, Create role, Create DB   | {}
    
    5: Never prompt for password
  • me          | Create DB                           | {}
    postgres    | Superuser, Create role, Create DB   | {}
    
    6:
    me          | Create DB                           | {}
    postgres    | Superuser, Create role, Create DB   | {}
    
    7: Force password prompt, which should happen automatically

We’ll connect to the default

postgres=# ALTER ROLE me CREATEDB;
0 database with the default login information and no option flags:

psql postgres

You’ll see that we’ve entered into a new connection. We’re now inside

postgres=# ALTER ROLE me CREATEDB;
1 in the
postgres=# ALTER ROLE me CREATEDB;
0 database. The prompt ends with a
postgres=# \q
1 to denote that we’re logged in as the superuser, or root:

postgres=#

Commands within

postgres=# ALTER ROLE me CREATEDB;
1 start with a backslash
postgres=# \q
3. To test our first command, we can check what database, user, and port we’ve connected to using the
postgres=# \q
4 command.

postgres=# \conninfo
You are connected to database "postgres" as user "your_username" via socket in "/tmp" at port "5432".

The reference table below includes a few common commands that we’ll use throughout this tutorial:

  • postgres=# \q
    
    5: Exit
    postgres=# ALTER ROLE me CREATEDB;
    
    1 connection
  • postgres=# \q
    
    7: Connect to a new database
  • postgres=# \q
    
    8: List all tables
  • postgres=# \q
    
    9: List all roles
  • psql -d postgres -U me
    
    0: List databases

Let’s create a new database and user so we’re not using the default accounts, which have superuser privileges.

Creating a role in Postgres

First, we’ll create a role called

psql -d postgres -U me
1 and give it a password of
psql -d postgres -U me
2. A role can function as a user or a group. In this case, we’ll use it as a user:

postgres=# CREATE ROLE me WITH LOGIN PASSWORD 'password';

We want

psql -d postgres -U me
1 to be able to create a database:

postgres=# ALTER ROLE me CREATEDB;

You can run

postgres=# \q
9 to list all roles and users:

me          | Create DB                           | {}
postgres    | Superuser, Create role, Create DB   | {}

Now, we want to create a database from the

psql -d postgres -U me
1 user. Exit from the default session with
postgres=# \q
5 for quit:

postgres=# \q

We’re back in our computer’s default terminal connection. Now, we’ll connect

postgres=# ALTER ROLE me CREATEDB;
0 with
psql -d postgres -U me
1:

psql -d postgres -U me

Instead of

psql -d postgres -U me
9, our prompt now shows
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
00 , meaning we’re no longer logged in as a superuser.

Creating a database in Postgres

We can create a database with the SQL command as follows:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
0

Use the

psql -d postgres -U me
0 command to see the available databases:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
1

Let’s connect to the new

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
02 database with
psql -d postgres -U me
1 using the
postgres=# \q
7 connect command:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
2

Our prompt now shows that we’re connected to

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
02.

Creating a table in Postgres

Finally, in the

postgres=# ALTER ROLE me CREATEDB;
1 command prompt, we’ll create a table called
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
07 with three fields, two
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
08 types, and an auto-incrementing
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
09 ID:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
3

Make sure not to use the backtick

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
10 character when creating and working with tables in PostgreSQL. While backticks are allowed in MySQL, they’re not valid in PostgreSQL. Also ensure that you do not have a trailing comma in the
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
11 command.


More great articles from LogRocket:

  • Don't miss a moment with The Replay, a curated newsletter from LogRocket
  • Learn how LogRocket's Galileo cuts through the noise to proactively resolve issues in your app
  • Use React's useEffect to optimize your application's performance
  • Switch between multiple versions of Node
  • Discover how to animate your React app with AnimXYZ
  • Explore Tauri, a new framework for building binaries
  • Compare NestJS vs. Express.js

Let’s add some data to work with by adding two entries to

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
07:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
4

Let’s make sure that the information above was correctly added by getting all entries in

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
07:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
5

Now, we have a user, database, table, and some data. We can begin building our Node.js RESTful API to connect to this data, stored in a PostgreSQL database.

At this point, we’re finished with all of our PostgreSQL tasks, and we can begin setting up our Node.js app and Express server.

Setting up an Express server

To set up a Node.js app and Express server, first create a directory for the project to live in:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
6

You can either run

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
14 to create a
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
15 file, or copy the code below into a
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
15 file:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
7

We’ll want to install Express for the server and node-postgres to connect to PostgreSQL:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
8

Now, we have our dependencies loaded into

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
17 and
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
15.

Create an

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
19 file, which we’ll use as the entry point for our server. At the top, we’ll require the
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
20 module, the built-in
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
21 middleware, and we’ll set our
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
22 and
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
23 variables:

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
9

We’ll tell a route to look for a

postgres=#
6 request on the root
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
25 URL and return some JSON:

psql postgres
0

Now, set the app to listen on the port you set:

psql postgres
1

From the command line, we can start the server by hitting

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
19:

psql postgres
2

Go to

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
27 in the URL bar of your browser, and you’ll see the JSON we set earlier:

psql postgres
3

The Express server is running now, but it’s only sending some static JSON data that we created. The next step is to connect to PostgreSQL from Node.js to be able to make dynamic queries.

Connecting to a Postgres database using a Client

A popular client for accessing Postgres databases is the pgAdmin client. The pgAdmin application is available for various platforms. If you want to have a graphical user interface for your Postgres databases, you can go to the download page and download the necessary package.

Creating and querying your database using pgAdmin is simple. You need to click on the Object option available on the top menu, select Create, and choose Database to create a new connection. All the databases are available on the side menu. You can query or run SQL queries efficiently by selecting the proper database:

Cara menggunakan line reader nodejs
Cara menggunakan line reader nodejs

Connecting to a Postgres database from Node.js

We’ll use the node-postgres module to create a pool of connections. Therefore, we don’t have to open and close a client each time we make a query.

A popular option for production pooling would be to use

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
28, a lightweight connection pooler for PostgreSQL.

Create a file called

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
29 and set up the configuration of your PostgreSQL connection:

psql postgres
4

In a production environment, you would want to put your configuration details in a separate file with restrictive permissions so that it is not accessible from version control. But, for the simplicity of this tutorial, we’ll keep it in the same file as the queries.

The aim of this tutorial is to allow CRUD operations,

postgres=#
6,
postgres=#
7,
postgres=#
8, and
postgres=#
9 on the API, which will run the corresponding database commands. To do so, we’ll set up a route for each endpoint and a function corresponding to each query.

Creating routes for CRUD operations

We’ll create six functions for six routes, as shown below. First, create all the functions for each route. Then, export the functions so they’re accessible:

  • postgres=#
    
    6:
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    25 |
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    36
  • postgres=#
    
    6:
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    38 |
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    39
  • postgres=#
    
    6:
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    41 |
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    42
  • postgres=#
    
    7:
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    38 |
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    45
  • postgres=#
    
    8:
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    41 |
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    48
  • postgres=#
    
    9:
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    41 |
    brew services start postgresql
    ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
    
    51

In

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
19, we made an
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
53 for the root endpoint with a function in it. Now, in
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
29, we’ll create endpoints that will display all users, display a single user, create a new user, update an existing user, and delete a user.

postgres=# 6 all users

Our first endpoint will be a

postgres=#
6 request. We can put the raw SQL that will touch the
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
02 database inside the
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
58. We’ll
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
59 all users and order by ID.

psql postgres
5

postgres=# 6 a single user by ID

For our

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
41 request, we’ll get the custom
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
62 parameter by the URL and use a
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
63 clause to display the result.

In the SQL query, we’re looking for

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
64. In this instance,
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
65 is a numbered placeholder that PostgreSQL uses natively instead of the
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
66 placeholder that you may recognize from other variations of SQL:

psql postgres
6

postgres=# 7 a new user

The API will take a

postgres=#
6 and
postgres=#
7 request to the
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
38 endpoint. In the
postgres=#
7 request, we’ll add a new user. In this function, we’re extracting the
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
72 and
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
73 properties from the request body and inserting the values with
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
74:

psql postgres
7

postgres=# 8 updated data in an existing user

The

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
41 endpoint will also take two HTTP requests, the
postgres=#
6 we created for
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
78 and a
postgres=#
8 to modify an existing user. For this query, we’ll combine what we learned in
postgres=#
6 and
postgres=#
7 to use the
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
82 clause.

It’s worth noting that

postgres=#
8 is idempotent, meaning the exact same call can be made over and over and will produce the same result.
postgres=#
8 is different than
postgres=#
7, in which the exact same call repeated will continuously make new users with the same data:

psql postgres
8

postgres=# 9 a user

Finally, we’ll use the

postgres=#
9 clause on
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
41 to delete a specific user by ID. This call is very similar to our
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
42 function:

psql postgres
9

Exporting CRUD functions in a REST API

To access these functions from

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
19, we’ll need to export them. We can do so with
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
91, creating an object of functions. Since we’re using the ES6 syntax, we can write
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
92 instead of
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
93 and so on:

postgres=#
0

Our complete

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
29 file is below:

postgres=#
1

Setting up CRUD functions in a REST API

Now that we have all of our queries, we need to pull them into the

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
19 file and make endpoint routes for all the query functions we created.

To get all the exported functions from

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
29, we’ll
brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
97 the file and assign it to a variable:

postgres=#
2

Now, for each endpoint, we’ll set the HTTP request method, the endpoint URL path, and the relevant function:

postgres=#
3

Below is our complete

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
19 file, the entry point of the API server:

postgres=#
4

With just these two files, we have a server, database, and our API all set up. You can start up the server by hitting

brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
19 again:

psql postgres
2

Now, if you go to

psql postgres
00 or
psql postgres
01, you’ll see the JSON response of the two
postgres=#
6 requests.

To test our

postgres=#
7,
postgres=#
8, and
postgres=#
9 requests, we can use a tool like Postman or a VS Code extension like Thunder Client to send the HTTP requests. You can also use curl, a command-line tool that is already available on your terminal.

Using a Postman or Thunder Client tool makes it simple to query endpoints with different HTTP methods. Simply enter your URL, choose the specific HTTP method, insert JSON value if the endpoint is a PUT or POST route, and hit send:

Cara menggunakan line reader nodejs
Cara menggunakan line reader nodejs

The example above shows sending a

postgres=#
7 request to the specified route. The
postgres=#
7 option suggests that it is a
postgres=#
7 request. The URL beside the method is the API endpoint, and the JSON content is the data to be sent to the endpoint. You can hit the different routes similarly.

Conclusion

You should now have a functioning API server that runs on Node.js and is hooked up to an active PostgreSQL database. In this tutorial, we learned how to install and set up PostgreSQL in the command line, create users, databases, and tables, and run SQL commands. We also learned how to create an Express server that can handle multiple HTTP methods and use the

psql postgres
09 module to connect to PostgreSQL from Node.js.

With this knowledge, you should be able to build on this API and utilize it for your own personal or professional development projects.

Bagaimana cara kerja Nodejs?

Cara Kerja Node.js Node.js menggunakan operasi I/O non-blocking. Artinya, saat client mengirim permintaan ke web server, event loop single-threaded Node JS akan mengambil permintaan tersebut lalu mengirimkannya ke worker thread untuk diproses.

Apakah node js sama dengan JavaScript?

Selain itu, Node.js juga mengusung model event-driven dan non-blocking I/O-nya sehingga lebih mampu menangani banyak proses secara bersamaan dibandingkan platform bersifat thread-based networking. Walau demikian, Node.js dan JavaScript bukanlah hal yang sama dan tidak dapat dibandingkan satu sama lain.

Bagaimanakah karakteristik api pada node js?

Semua API dari Node.js bersifat asynchronous, artinya tidak memblokir proses lain sembari menunggu satu proses selesai.

Package JSON untuk apa?

Apa Itu package.json? Setiap proyek npm berisi package.json, yaitu file yang terletak di direktori root dan berisi metadata proyek atau package NPM, seperti versi dan kontributor package. File package.json memudahkan proses identifikasi, pengelolaan, dan penginstalan package.