Skip to content

Instantly share code, notes, and snippets.

View sameralsayed's full-sized avatar

Samer Saeid sameralsayed

View GitHub Profile
@sameralsayed
sameralsayed / PHP Array Introduction
Created June 27, 2022 17:11
The array functions allow you to access and manipulate arrays.
PHP Array Introduction
The array functions allow you to access and manipulate arrays.
Simple and multi-dimensional arrays are supported.
Installation
The array functions are part of the PHP core. There is no installation needed to use these functions.
PHP Array Functions
Function Description
@sameralsayed
sameralsayed / GruntJS
Created June 16, 2022 20:32
GruntJS is a JavaScript task runner. It can be used to automate various tasks for your application with minimum effort which increases the developer's productivity.
GruntJS
GruntJS is a JavaScript task runner. It can be used to automate various tasks for your application with minimum effort which increases the developer's productivity.
GruntJS includes plugins for various tasks. For example, nodemon plugin automatically restarts the node server whenever any JavaScript file changes in your application. So, you don't have to stop and restart the node server manually everytime you modify JavaScript. The grunt-contrib-cssmin plugin can be used to compress CSS files. The grunt-jsfmt plugin can be used to format, search or rewrite the JavaScript in your application.
Visit gruntjs.com/plugins listing to know about all the available plugins in GruntJS.
GruntJS Installation
In order to use GruntJS, you first need to install Grunt's command line interface (CLI) globally using following npm command.
@sameralsayed
sameralsayed / Vash Template Engine
Created June 16, 2022 20:31
In this section, you will learn about Vash template engine and how to use it in Node.js using Express.js.
Vash Template Engine
In this section, you will learn about Vash template engine and how to use it in Node.js using Express.js.
Vash is a template view engine that uses Razor Syntax. So, this template engine will look familiar to people who have experience in ASP.Net MVC.
Install Vash using NPM as below.
npm install vash --save
Now, let's see how to render simple vash template using Express.js.
@sameralsayed
sameralsayed / Jade Template Engine
Created June 16, 2022 20:31
In this section, you will learn how to use Jade template engine in Node.js application using Express.js.
Jade Template Engine
In this section, you will learn how to use Jade template engine in Node.js application using Express.js.
Jade is a template engine for Node.js. Jade syntax is easy to learn. It uses whitespace and indentation as a part of the syntax.
Install jade into your project using NPM as below.
npm install jade
Jade template must be written inside .jade file. And all .jade files must be put inside views folder in the root folder of Node.js application.
@sameralsayed
sameralsayed / Template Engines for Node.js
Created June 16, 2022 20:30
Template engine helps us to create an HTML template with minimal code. Also, it can inject data into HTML template at client side and produce the final HTML.
Template Engines for Node.js
Template engine helps us to create an HTML template with minimal code. Also, it can inject data into HTML template at client side and produce the final HTML.
The following figure illustrates how template engine works in Node.js.
Template Engine
As per the above figure, client-side browser loads HTML template, JSON/XML data and template engine library from the server. Template engine produces the final HTML using template and data in client's browser. However, some HTML templates process data and generate final HTML page at server side also.
There are many template engines available for Node.js. Each template engine uses a different language to define HTML template and inject data into it.
@sameralsayed
sameralsayed / Access MongoDB in Node.js
Created June 16, 2022 20:30
Learn how to access document-based database MongoDB using Node.js in this section.
Access MongoDB in Node.js
Learn how to access document-based database MongoDB using Node.js in this section.
In order to access MongoDB database, we need to install MongoDB drivers. To install native mongodb drivers using NPM, open command prompt and write the following command to install MongoDB driver in your application.
npm install mongodb --save
This will include mongodb folder inside node_modules folder. Now, start the MongoDB server using the following command. (Assuming that your MongoDB database is at C:\MyNodeJSConsoleApp\MyMongoDB folder.)
mongod -dbpath C:\MyNodeJSConsoleApp\MyMongoDB
Connecting MongoDB
@sameralsayed
sameralsayed / Access SQL Server in Node.js
Created June 16, 2022 20:29
Learn how to access relational database MS SQL Server 2012 in Node.js application using Express.js in this section.
Access SQL Server in Node.js
Learn how to access relational database MS SQL Server 2012 in Node.js application using Express.js in this section.
In order to access MS SQL database, we need to install drivers for it. There are many drivers available for SQL server in NPM. We will use mssql driver here.
Install Driver
Install mssql driver using npm command, npm install mssql in the command prompt. This will add mssql module folder in node_modules folder in your Node.js application. This tutorial uses mssql v2.3.1, which is latest version as of now.
After installing the driver, we are ready to access MS SQL server database. We will connect to a local SQLExpress database server and fetch all the records from Student table in SchoolDB database shown below.
@sameralsayed
sameralsayed / Data Access in Node.js
Created June 16, 2022 20:29
Node.js supports all kinds of databases no matter if it is a relational database or NoSQL database. However, NoSQL databases like MongoDb are the best fit with Node.js.
Data Access in Node.js
Node.js supports all kinds of databases no matter if it is a relational database or NoSQL database. However, NoSQL databases like MongoDb are the best fit with Node.js.
To access the database from Node.js, you first need to install drivers for the database you want to use.
The following table lists important relational databases and respective drivers.
Relational Databases Driver NPM Command
MS SQL Server mssql npm install mssql
Oracle oracledb npm install oracledb
@sameralsayed
sameralsayed / Serving Static Resources in Node.js
Created June 16, 2022 20:29
In this section, you will learn how to serve static resources like images, css, JavaScript or other static files using Express.js and node-static module.
Serving Static Resources in Node.js
In this section, you will learn how to serve static resources like images, css, JavaScript or other static files using Express.js and node-static module.
Serve Static Resources using Express.js
It is easy to serve static files using built-in middleware in Express.js called express.static. Using express.static() method, you can server static resources directly by specifying the folder name where you have stored your static resources.
The following example serves static resources from the public folder under the root folder of your application.
server.js Copy
var express = require('express');
@sameralsayed
sameralsayed / Express.js Web Application
Created June 16, 2022 20:28
In this section, you will learn how to create a web application using Express.js.
Express.js Web Application
In this section, you will learn how to create a web application using Express.js.
Express.js provides an easy way to create web server and render HTML pages for different HTTP requests by configuring routes for your application.
Web Server
First of all, import the Express.js module and create the web server as shown below.
app.js: Express.js Web Server Copy
var express = require('express');