♊️ GemiNews 🗞️ (dev)

Demo 1: Embeddings + Recommendation Demo 2: Bella RAGa Demo 3: NewRetriever Demo 4: Assistant function calling

🗞️Building a RESTful API with Node, Express, and MongoDB

🗿Semantically Similar Articles (by :title_embedding)

Building a RESTful API with Node, Express, and MongoDB

2018-02-27 - Kevin Randles (from Kevin Randles on Medium)

As I’ve become more familiar with JavaScript, and more aware of the possibilities it offers, I’ve been curious about the recent trend of full-stack Javascript applications — “recent” being a relative term here, as backend JS frameworks started appearing shortly after the release of Node.js in 2009. Node, for those unfamiliar with it, is a JavaScript runtime built on Google’s V8 engine (this is what runs JavaScript in Chrome), that allows you to run JS code on a server, uncoupled from the browser. If you’d like to follow along with my code examples here, you’ll need to install Node, which you can find here.For my first foray into JavaScript on the backend, I decided to take a look at Express, one of the most popular JS web frameworks. Released in 2010, Express is a minimalist, unopinionated framework that extends the basic HTTP server functionality available in Node. As a minimalist framework, the feature set it provides out-of-the-box is limited compared to many other frameworks, but as a result of its popularity, there’s an extensive collection of middleware available to add functionality as the requirements of your project necessitate.This blog post will take a brief look at how to set up an Express server as a simple API endpoint using a MongoDB database for storage. I chose MongoDB as an opportunity to get more familiar with NoSQL databases, a concept I’ve heard about repeatedly, but that I’ve only had a vague understanding of — if you’re in the same position, and you’re familiar with SQL databases, you might be curious why we don’t have to define schema or migrations before adding data to our database. Very broadly, MongoDB is a document database that stores records as collections of documents, which are JSON-like objects composed of key:value pairs, with dynamic schema. (For a more in-depth intro to MongoDB, see here)Getting StartedAssuming you’ve already installed Node from the link above, the first steps are creating a project folder and getting it ready. My example here will be an API for data about beers, because beer is great, so I’ll take the obvious route and call this app “beerdb”:$ mkdir beerdb$ cd beerdb$ npm initThe “npm init” command will ask you for a lot of info here, but you can just accept the default values, with the exception of the entry point, which i’ve changed to “server.js”Next, we need to add a few packages to our project — Express, for obvious reasons, the MongoDB client to interface with our database, and body-parser, for parsing HTTP requests as JSON. Installing Nodemon in your dev environment is also highly recommended — it will keep an eye on your project folder and restart your server whenever your files change during development.$ npm install --save express$ npm install --save mongodb@2.2.19$ npm install --save body-parser$ npm install --save-dev nodemonYour package.json file should look like the following, with one exception — you’ll need to add the highlighted text under “scripts” to allow Nodemon to do its thing.With our packages installed, we can now get started on setting up our server — create a file called “server.js” in the root folder of your project, and add the following code:At this point, you can run “npm run dev” (or “npm start” if you’re not using Nodemon) in your terminal and, assuming you’ve done everything correctly your server should be up and running:We have a server! But without any routes, it can’t do much of anything, so let’s make some routes — first, to keep our project logically organized, create an “app” folder in the root folder of your project then, within “app”, a “routes” folder containing two files — “index.js”, where we’ll export our routes for use by the server and “beer_routes.js”, where we’ll define our routes. Express requires our routes to be wrapped in a function, which needs to have our server instance and database (we don’t have a database yet, but I’ll get to that shortly) passed to it as arguments, which looks something like this:To break down the code here a little:Line 5 uses Express’ methods to define the type of request, in this case a POST, and the route — “/beers”Lines 6–10 grab the details about our new beer from the body of the POST request and save them to a variableLines 13–17 tell our database which collection we want (beers, we always want beers), and what to to that collection, insert a new document then, depending on the database’s response, returns the appropriate response to the client.In index.js, we just need to package our routes (we only have one set of routes at the moment, but as our application grows we would collect additional routes we create here) for export.Now we have a route that should allow us to add new beers to our database, but we don’t have a database! We also don’t have a way to send data to this route to find out if it works, so it’s time for a brief digression…Setting up MongoDBIf you’d like, you can host your database locally, and there are plenty of tutorials like this one at W3Schools, but for expediency I’m using mLab. Their free tier gives you up to 500MB of storage, and your choice of hosting on AWS or Azure. Once you’ve created an account, create a new database and add a database user then, from your newly created database’s page, you’ll need to grab the link that looks something like this:Create a new folder called “config” in your project’s root folder, then a “db.js” file within “config”, where you’ll store and export your database’s URL, substituting your database user’s credentials here:Don’t put this file on GitHub!!!Back in “server.js”, we need to import our db.js file and configure MongoClient to connect to the new database (this replaces our earlier call to app.listen)Now we should be ready to add some beers to our database, and unless you’ve already built a front-end, the best way to make sure everything is set up properly is with Postman.Configure your POST request as shown above, send it off, and if all is well you should get a response containing your newly created document:Mmmm, Double ShotNow that we have something in our database, it’s time to build out the rest of our routes, because what good is a database you can only add things to?This is our GET route — if you understand what’s going on in the POST route, this isn’t much different. We’re using the findOne method to look up a document by ID, but keep in mind MongoDB expects to receive the ID as an instance of ObjectID, not a string, so we need one more line at the top of of beer_routes.js:The DELETE and PUT/PATCH routes are very similar to the GET and POST routes:And finally, a second GET route, this one to return the index of all the beers:With that, you’ve created a RESTful API endpoint with Express!I’ll post this example code on GitHub shortly, so if you’re reading this I haven’t done that yet…

[Technology] 🌎 https://medium.com/@krandles/building-a-restful-api-with-node-express-and-mongodb-9a78119973aa?source=rss-d451d084d34a------2 [🧠] [v2] article_embedding_description: {:llm_project_id=>"Unavailable", :llm_dimensions=>nil, :article_size=>9310, :llm_embeddings_model_name=>"textembedding-gecko"}
[🧠] [v1/3] title_embedding_description: {:ricc_notes=>"[embed-v3] Fixed on 9oct24. Only seems incompatible at first glance with embed v1.", :llm_project_id=>"unavailable possibly not using Vertex", :llm_dimensions=>nil, :article_size=>9310, :poly_field=>"title", :llm_embeddings_model_name=>"textembedding-gecko"}
[🧠] [v1/3] summary_embedding_description:
[🧠] As per bug https://github.com/palladius/gemini-news-crawler/issues/4 we can state this article belongs to titile/summary version: v3 (very few articles updated on 9oct24)

🗿article.to_s

------------------------------
Title: Building a RESTful API with Node, Express, and MongoDB
[content]
As I’ve become more familiar with JavaScript, and more aware of the possibilities it offers, I’ve been curious about the recent trend of full-stack Javascript applications — “recent” being a relative term here, as backend JS frameworks started appearing shortly after the release of Node.js in 2009. Node, for those unfamiliar with it, is a JavaScript runtime built on Google’s V8 engine (this is what runs JavaScript in Chrome), that allows you to run JS code on a server, uncoupled from the browser. If you’d like to follow along with my code examples here, you’ll need to install Node, which you can find here.For my first foray into JavaScript on the backend, I decided to take a look at Express, one of the most popular JS web frameworks. Released in 2010, Express is a minimalist, unopinionated framework that extends the basic HTTP server functionality available in Node. As a minimalist framework, the feature set it provides out-of-the-box is limited compared to many other frameworks, but as a result of its popularity, there’s an extensive collection of middleware available to add functionality as the requirements of your project necessitate.This blog post will take a brief look at how to set up an Express server as a simple API endpoint using a MongoDB database for storage. I chose MongoDB as an opportunity to get more familiar with NoSQL databases, a concept I’ve heard about repeatedly, but that I’ve only had a vague understanding of — if you’re in the same position, and you’re familiar with SQL databases, you might be curious why we don’t have to define schema or migrations before adding data to our database. Very broadly, MongoDB is a document database that stores records as collections of documents, which are JSON-like objects composed of key:value pairs, with dynamic schema. (For a more in-depth intro to MongoDB, see here)Getting StartedAssuming you’ve already installed Node from the link above, the first steps are creating a project folder and getting it ready. My example here will be an API for data about beers, because beer is great, so I’ll take the obvious route and call this app “beerdb”:$ mkdir beerdb$ cd beerdb$ npm initThe “npm init” command will ask you for a lot of info here, but you can just accept the default values, with the exception of the entry point, which i’ve changed to “server.js”Next, we need to add a few packages to our project — Express, for obvious reasons, the MongoDB client to interface with our database, and body-parser, for parsing HTTP requests as JSON. Installing Nodemon in your dev environment is also highly recommended — it will keep an eye on your project folder and restart your server whenever your files change during development.$ npm install --save express$ npm install --save mongodb@2.2.19$ npm install --save body-parser$ npm install --save-dev nodemonYour package.json file should look like the following, with one exception — you’ll need to add the highlighted text under “scripts” to allow Nodemon to do its thing.With our packages installed, we can now get started on setting up our server — create a file called “server.js” in the root folder of your project, and add the following code:At this point, you can run “npm run dev” (or “npm start” if you’re not using Nodemon) in your terminal and, assuming you’ve done everything correctly your server should be up and running:We have a server! But without any routes, it can’t do much of anything, so let’s make some routes — first, to keep our project logically organized, create an “app” folder in the root folder of your project then, within “app”, a “routes” folder containing two files — “index.js”, where we’ll export our routes for use by the server and “beer_routes.js”, where we’ll define our routes. Express requires our routes to be wrapped in a function, which needs to have our server instance and database (we don’t have a database yet, but I’ll get to that shortly) passed to it as arguments, which looks something like this:To break down the code here a little:Line 5 uses Express’ methods to define the type of request, in this case a POST, and the route — “/beers”Lines 6–10 grab the details about our new beer from the body of the POST request and save them to a variableLines 13–17 tell our database which collection we want (beers, we always want beers), and what to to that collection, insert a new document then, depending on the database’s response, returns the appropriate response to the client.In index.js, we just need to package our routes (we only have one set of routes at the moment, but as our application grows we would collect additional routes we create here) for export.Now we have a route that should allow us to add new beers to our database, but we don’t have a database! We also don’t have a way to send data to this route to find out if it works, so it’s time for a brief digression…Setting up MongoDBIf you’d like, you can host your database locally, and there are plenty of tutorials like this one at W3Schools, but for expediency I’m using mLab. Their free tier gives you up to 500MB of storage, and your choice of hosting on AWS or Azure. Once you’ve created an account, create a new database and add a database user then, from your newly created database’s page, you’ll need to grab the link that looks something like this:Create a new folder called “config” in your project’s root folder, then a “db.js” file within “config”, where you’ll store and export your database’s URL, substituting your database user’s credentials here:Don’t put this file on GitHub!!!Back in “server.js”, we need to import our db.js file and configure MongoClient to connect to the new database (this replaces our earlier call to app.listen)Now we should be ready to add some beers to our database, and unless you’ve already built a front-end, the best way to make sure everything is set up properly is with Postman.Configure your POST request as shown above, send it off, and if all is well you should get a response containing your newly created document:Mmmm, Double ShotNow that we have something in our database, it’s time to build out the rest of our routes, because what good is a database you can only add things to?This is our GET route — if you understand what’s going on in the POST route, this isn’t much different. We’re using the findOne method to look up a document by ID, but keep in mind MongoDB expects to receive the ID as an instance of ObjectID, not a string, so we need one more line at the top of of beer_routes.js:The DELETE and PUT/PATCH routes are very similar to the GET and POST routes:And finally, a second GET route, this one to return the index of all the beers:With that, you’ve created a RESTful API endpoint with Express!I’ll post this example code on GitHub shortly, so if you’re reading this I haven’t done that yet…
[/content]

Author: Kevin Randles
PublishedDate: 2018-02-27
Category: Technology
NewsPaper: Kevin Randles on Medium
Tags: javascript
{"id"=>4221,
"title"=>"Building a RESTful API with Node, Express, and MongoDB",
"summary"=>nil,
"content"=>"

As I’ve become more familiar with JavaScript, and more aware of the possibilities it offers, I’ve been curious about the recent trend of full-stack Javascript applications — “recent” being a relative term here, as backend JS frameworks started appearing shortly after the release of Node.js in 2009. Node, for those unfamiliar with it, is a JavaScript runtime built on Google’s V8 engine (this is what runs JavaScript in Chrome), that allows you to run JS code on a server, uncoupled from the browser. If you’d like to follow along with my code examples here, you’ll need to install Node, which you can find here.

For my first foray into JavaScript on the backend, I decided to take a look at Express, one of the most popular JS web frameworks. Released in 2010, Express is a minimalist, unopinionated framework that extends the basic HTTP server functionality available in Node. As a minimalist framework, the feature set it provides out-of-the-box is limited compared to many other frameworks, but as a result of its popularity, there’s an extensive collection of middleware available to add functionality as the requirements of your project necessitate.

This blog post will take a brief look at how to set up an Express server as a simple API endpoint using a MongoDB database for storage. I chose MongoDB as an opportunity to get more familiar with NoSQL databases, a concept I’ve heard about repeatedly, but that I’ve only had a vague understanding of — if you’re in the same position, and you’re familiar with SQL databases, you might be curious why we don’t have to define schema or migrations before adding data to our database. Very broadly, MongoDB is a document database that stores records as collections of documents, which are JSON-like objects composed of key:value pairs, with dynamic schema. (For a more in-depth intro to MongoDB, see here)

Getting Started

Assuming you’ve already installed Node from the link above, the first steps are creating a project folder and getting it ready. My example here will be an API for data about beers, because beer is great, so I’ll take the obvious route and call this app “beerdb”:

$ mkdir beerdb
$ cd beerdb
$ npm init

The “npm init” command will ask you for a lot of info here, but you can just accept the default values, with the exception of the entry point, which i’ve changed to “server.js”

Next, we need to add a few packages to our project — Express, for obvious reasons, the MongoDB client to interface with our database, and body-parser, for parsing HTTP requests as JSON. Installing Nodemon in your dev environment is also highly recommended — it will keep an eye on your project folder and restart your server whenever your files change during development.

$ npm install --save express
$ npm install --save mongodb@2.2.19
$ npm install --save body-parser
$ npm install --save-dev nodemon

Your package.json file should look like the following, with one exception — you’ll need to add the highlighted text under “scripts” to allow Nodemon to do its thing.

\"\"

With our packages installed, we can now get started on setting up our server — create a file called “server.js” in the root folder of your project, and add the following code:

\"\"

At this point, you can run “npm run dev” (or “npm start” if you’re not using Nodemon) in your terminal and, assuming you’ve done everything correctly your server should be up and running:

\"\"

We have a server! But without any routes, it can’t do much of anything, so let’s make some routes — first, to keep our project logically organized, create an “app” folder in the root folder of your project then, within “app”, a “routes” folder containing two files — “index.js”, where we’ll export our routes for use by the server and “beer_routes.js”, where we’ll define our routes. Express requires our routes to be wrapped in a function, which needs to have our server instance and database (we don’t have a database yet, but I’ll get to that shortly) passed to it as arguments, which looks something like this:

\"\"

To break down the code here a little:

  • Line 5 uses Express’ methods to define the type of request, in this case a POST, and the route — “/beers”
  • Lines 6–10 grab the details about our new beer from the body of the POST request and save them to a variable
  • Lines 13–17 tell our database which collection we want (beers, we always want beers), and what to to that collection, insert a new document then, depending on the database’s response, returns the appropriate response to the client.

In index.js, we just need to package our routes (we only have one set of routes at the moment, but as our application grows we would collect additional routes we create here) for export.

\"\"

Now we have a route that should allow us to add new beers to our database, but we don’t have a database! We also don’t have a way to send data to this route to find out if it works, so it’s time for a brief digression…

Setting up MongoDB

If you’d like, you can host your database locally, and there are plenty of tutorials like this one at W3Schools, but for expediency I’m using mLab. Their free tier gives you up to 500MB of storage, and your choice of hosting on AWS or Azure. Once you’ve created an account, create a new database and add a database user then, from your newly created database’s page, you’ll need to grab the link that looks something like this:

\"\"

Create a new folder called “config” in your project’s root folder, then a “db.js” file within “config”, where you’ll store and export your database’s URL, substituting your database user’s credentials here:

\"\"
Don’t put this file on GitHub!!!

Back in “server.js”, we need to import our db.js file and configure MongoClient to connect to the new database (this replaces our earlier call to app.listen)

\"\"

Now we should be ready to add some beers to our database, and unless you’ve already built a front-end, the best way to make sure everything is set up properly is with Postman.

\"\"

Configure your POST request as shown above, send it off, and if all is well you should get a response containing your newly created document:

\"\"
Mmmm, Double Shot

Now that we have something in our database, it’s time to build out the rest of our routes, because what good is a database you can only add things to?

\"\"

This is our GET route — if you understand what’s going on in the POST route, this isn’t much different. We’re using the findOne method to look up a document by ID, but keep in mind MongoDB expects to receive the ID as an instance of ObjectID, not a string, so we need one more line at the top of of beer_routes.js:

\"\"

The DELETE and PUT/PATCH routes are very similar to the GET and POST routes:

\"\"

And finally, a second GET route, this one to return the index of all the beers:

\"\"

With that, you’ve created a RESTful API endpoint with Express!

I’ll post this example code on GitHub shortly, so if you’re reading this I haven’t done that yet…

\"\"",
"author"=>"Kevin Randles",
"link"=>"https://medium.com/@krandles/building-a-restful-api-with-node-express-and-mongodb-9a78119973aa?source=rss-d451d084d34a------2",
"published_date"=>Tue, 27 Feb 2018 19:55:40.000000000 UTC +00:00,
"image_url"=>nil,
"feed_url"=>"https://medium.com/@krandles/building-a-restful-api-with-node-express-and-mongodb-9a78119973aa?source=rss-d451d084d34a------2",
"language"=>nil,
"active"=>true,
"ricc_source"=>"feedjira::v1",
"created_at"=>Mon, 01 Apr 2024 20:13:28.619059000 UTC +00:00,
"updated_at"=>Mon, 21 Oct 2024 18:02:54.259466000 UTC +00:00,
"newspaper"=>"Kevin Randles on Medium",
"macro_region"=>"Technology"}
Edit this article
Back to articles