Jax-RS REST Client example demonstrating how to GET a resource with a query parameter. The resource is secured with Basic Authentication.
Cleaner code in NodeJs with async-await - Mongoose calls example
A lot has been written already about the transition from callbacks
to promises and now to the new
async/await
1 feature in ES7.
"The purpose of `async/await` functions is to simplify the behavior of using promises synchronously and to perform some behavior on a group of `Promises`. Just as `Promises` are similar to structured callbacks, `async/await` is similar to combining generators and promises."[^1]
In this blog post I present what this code “upgrade” meant for CRUD operations performed on dev bookmarks. I use Moongoose in an ExpressJS/NodeJS backend to perform the operations against a MongoDB database.
In a later refactoring the database access part has been decoupled from ExpressJS and moved to separate service functions. Thanks to unified error handling the database errors are now handled centrally.
Source code for Codever.dev is available on Github -
Star
How to redirect domain to www url with Nginx
This post presents the snippet from the Nginx configuration that redirects all request to https://www.codever.dev:
# redirect HTTP to www
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name bookmarks.dev www.codever.dev;
return 301 https://www.bookmarks.dev$request_uri;
}
# redirect HTTPS to wwww
server {
listen 443;
server_name bookmarks.dev;
return 301 https://www.bookmarks.dev$request_uri;
}
Note the missing
www.codever.dev
in the secondserver/server_name
entry, to avoid an infinite loop
Lots of other good Nginx resources can be found if you search for the nginx tag on codingmarks: https://www.codever.dev?q=[nginx]
Source code for Codever.dev is available on Github -
Star
How to insert a document in mongodb in java - example
This blog post presents a simple example showing how to insert a document in mongodb, in the Java language.
The code samples are taken from the Free-Programming-Books-Importer project, available on Github
Use MongoDB Java Driver
Add the java mongo driver to your class path, or if you use maven to the dependencies
in your pom file:
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.2.2</version>
</dependency>
......
</dependencies>
Prepare the Mongo Client
MongoClientURI connectionString = new MongoClientURI("mongodb://codingpedia:codingpedia@localhost:27017/codingpedia-bookmarks");
MongoClient mongoClient = new MongoClient(connectionString);
Free Programming Books are now on Bookmarks.dev
I am really happy to announce that we’ve reached and surpassed our goal of 1Mb of #DevBookmarks. This was possible by importing the free programming books from the Free-Programming-Books project, which is one of the most starred repository on GitHub.
You can look for all imported free books by typing [free-programming-books]
(tag filtering) in the search bar, or by provide it as query parameter:
https://www.codever.dev/search?q=Bfree-programming-books%5D.
Either way you should get a result similar to the following:
