Full Trust European Hosting

BLOG about Full Trust Hosting and Its Technology - Dedicated to European Windows Hosting Customer

Europe mySQL Hosting - HostForLIFEASP.NET :: Setup Local Database In SQL Server

clock April 19, 2022 10:12 by author Peter

Requirements
Microsoft SQL Server. If not installed, go to the official site and download and install the setup file.

STEP 1

    Go to Start and search for Microsoft SQL Server.
    You can find an option for Microsoft SQL Server Management Studio.
    Click on it to open the SQL Management Studio.

STEP 2

    To create a local database, you need a Server first.
    While installing the SQL Server, you would have set a user which will act as the Server.
    Select the Server and also ensure that the credentials you are providing in the authentication processes are right.
    After entering all the details, click on the "Connect" button.
    If the connection fails, check if the Server is in running state.
    If it is not running, just right click on the Local Sever and click on the Start option.
    You can find the Server in the Local Server option of the Registered Servers.
    To view the registered servers, go to VIEW -> REGISTERED SERVERS.

STEP 3

  1. Now, you are connected to the Server, so can you create a database.
  2. In the Object Explorer, find the Databases folder.
  3. Simply right click on it and select new database option.
  4. This will initiate the new database creation.

Step 4

  1. You will see a window when clicked on the new database option.There, you can add the new database.
  2. You can add more than one database on the same Server.
  3. Click on the "Add" button to add a new database.
  4. If you want to remove any database, just click on the "Remove" button.
  5. Once you are finished with adding your required number of databases, click on the OK button.
STEP 5
    Now, you can see a new database appearing in the database menu in the Object Explorer. You can now use this database for your local storage.

So, this was the process for local database creation using MS SQL
In my next write-up, I will explain the steps for writing queries and creating tables. Thank you.



Node.js Hosting Europe - HostForLIFE :: How To Make Middleware In Node.js

clock April 13, 2022 09:13 by author Peter

Node.js is a backend framework based on Javascript. It is said to be 10x faster as compared to other backend languages as it is developed keeping in mind Javascript runtime browser compatibility.


Node.js uses non-blocking I/O, the main reason for making it lightweight and efficient.

Moreover, it is open-source, cross-platform runtime environment for developing server-side applications.

Some other backend languages,
    PYTHON.
    PHP LARVEL.
    GOLANG.
    C#.
    RUBY.
    JAVA.

But the fact is node.js is said to be fastest among all as it totally works on npm (node package manager) which gives developers multiple tools and modules to use thus helping in execution of code in the fastest way as possible.
Requirements

Starting with nodejs is an easy task just by downloading/installing it, if not yet downloaded, here’s the link to download.

After installing node.js on your  local computer or laptop, to check node gets properly installed on a particular system, open command prompt and type node -v to check.

There you go😊. Creating Middleware using Express Apps - Overview

Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle. The next function is in the Express router which, when invoked, executes the middleware succeeding the current middleware.

The following task can be performed through middleware,
    Execute any code.
    Make changes to the request and the response objects.
    End the request-response cycle.
    Call the next middleware in the stack.

If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.

The following figure shows the elements of a middleware function call,

HTTP method.
Path (route) for which the middleware function applies
The middleware function.
Callback argument to the middleware function.
HTTP response argument to the middleware.
HTTP request argument to the middleware

Starting with Express 5, middleware functions that return a Promise will call next(value) when they reject or throw an error.

next will be called with either the rejected value or the thrown Error.

Example
Here is an example of a simple “Hello World” Express application. The use of this article will define and add three middleware functions to the application: one called myLogger that prints a simple log message, one called requestTime that displays the timestamp of the HTTP request, and one called validateCookies that validates incoming cookies.
const express = require('express')
const app = express()
app.get('/', (req, res) => {
    res.send('Hello World!')
})
app.listen(3000)


Middleware function myLogger
Here is a simple example of a middleware function called “myLogger”. This function just prints “LOGGED” when a request to the app passes through it. The middleware function is assigned to a variable named myLogger.
const myLogger = function(req, res, next) {
    console.log('LOGGED')
    next()
}


Notice the call above to next(). Calling this function invokes the next middleware function in the app. The next() function is not a part of the Node.js or Express API but is the third argument that is passed to the middleware function. The next() function could be named anything, but by convention it is always named “next”. To avoid confusion, always use this convention.

To load the middleware function, call app.use(), specifying the middleware function. For example, the following code loads the myLogger middleware function before the route to the root path (/).
const express = require('express')
const app = express()
const myLogger = function(req, res, next) {
    console.log('LOGGED')
    next()
}
app.use(myLogger)
app.get('/', (req, res) => {
    res.send('Hello World!')
})
app.listen(3000)


Every time the app receives a request, it prints the message “LOGGED” to the terminal.

The order of middleware loading is important: middleware functions that are loaded first are also executed first.

If myLogger is loaded after the route to the root path, the request never reaches it and the app doesn’t print “LOGGED”, because the route handler of the root path terminates the request-response cycle.

The middleware function myLogger simply prints a message, then passes on the request to the next middleware function in the stack by calling the next() function.

Middleware function requestTime

Next, we’ll create a middleware function called “requestTime” and add a property called requestTime to the request object.
const requestTime = function(req, res, next) {
    req.requestTime = Date.now()
    next()
}


The app now uses the requestTime middleware function. Also, the callback function of the root path route uses the property that the middleware function adds to req (the request object).
const express = require('express')
const app = express()
const requestTime = function(req, res, next) {
    req.requestTime = Date.now()
    next()
}
app.use(requestTime)
app.get('/', (req, res) => {
    let responseText = 'Hello World!<br>'
    responseText += `<small>Requested at: ${req.requestTime}</small>`
    res.send(responseText)
})
app.listen(3000)


When you make a request to the root of the app, the app now displays the timestamp of your request in the browser.
Middleware function validateCookies

Finally, we’ll create a middleware function that validates incoming cookies and sends a 400 response if cookies are invalid.

Here’s an example function that validates cookies with an external async service.
async function cookieValidator(cookies) {
    try {
        await externallyValidateCookie(cookies.testCookie)
    } catch {
        throw new Error('Invalid cookies')
    }
}


Here we use the cookie-parser middleware to parse incoming cookies off the req object and pass them to our cookieValidator function. The validateCookies middleware returns a Promise that upon rejection will automatically trigger our error handler.
const express = require('express')
const cookieParser = require('cookie-parser')
const cookieValidator = require('./cookieValidator')
const app = express()
async function validateCookies(req, res, next) {
    await cookieValidator(req.cookies)
    next()
}
app.use(cookieParser())
app.use(validateCookies)
// error handler
app.use((err, req, res, next) => {
    res.status(400).send(err.message)
})
app.listen(3000)


Note how next() is called after await cookieValidator(req.cookies). This ensures that if cookieValidator resolves, the next middleware in the stack will get called. If you pass anything to the next() function (except the string 'route' or 'router'), Express regards the current request as being an error and will skip any remaining non-error handling routing and middleware functions.

Because you have access to the request object, the response object, the next middleware function in the stack, and the whole Node.js API, the possibilities with middleware functions are endless.

Configurable middleware

If you need your middleware to be configurable, export a function that accepts an options object or other parameters, which, then returns the middleware implementation based on the input parameters.

File: my-middleware.js
module.exports = function(options) {
    return function(req, res, next) {
        // Implement the middleware function based on the options object
        next()
    }
}
// The middleware can now be used as shown below.
const mw = require('./my-middleware.js')
app.use(mw({
    option1: '1',
    option2: '2'
}))


Summary
Depending on our short definition, middleware can be created for purposes such as database transactions, transactions between application servers, message controls, and authentication. Structures such as soap, rest, and JSON can be used in the communication of applications. The middleware acts according to all these purposes.



Europe mySQL Hosting - HostForLIFEASP.NET :: Compare JSON In MySql

clock April 5, 2022 09:39 by author Peter

Scenario
I have a small need where I have to compare 2 JSON Records in MySql. The query will check both JSONs and will return some count if there is any modification in the new JSON record.


To help you understand easily, I am adding small JSON although I have some large JSON to compare.

To start, first create a test table.Compare JSON In MySql
CREATE TABLE `table1` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `json_col` json DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

To insert dummy JSON record, please use Insert query.

INSERT INTO `iaptsdb`.`table1`
(`json_col`)
VALUES('{"Unit": "109", "LastName": "Smith", "PersonId": "t0077508", "FirstName": "Karleen"}');

Now you have one record in table1.
Check by selecting this table1.

 

Now we will add our JSON Compare query to check JSON with above record.
We will update FirstName in new JSON and then we will compare it with above record.

I have SET FirstName to "John" and compare it to Record Id 1 in table1.

As FirstName is not equal to FirstName Record Id 1, hence it will reflect no record.**

set @some_json = '{"Unit": "109", "LastName": "Smith", "PersonId": "t0077508", "FirstName": "John"}';
select * from  table1
 WHERE
 CAST(json_col as JSON) = CAST(@some_json as JSON);


If we add "Karleen" as FirstName in JSON, the query will compare and as record is same, so it reflect one record.


P.S: Sequence in JSON Key:Value doesn't matter. :)
If you have implemented other approaches, please share them in the comments as well.



About HostForLIFE

HostForLIFE is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Tag cloud

Sign in