Simply, middleware are just functions for handling requests from client. Each
request can have be associated with a stack of middleware. That means when you
have several of handler functions associated with one request, they will be
executed sequentially. Back to the example from my previous post
In this example, there is only one middleware (handler) function for the GET
request to /. Now we will add one more middleware, or maybe more if you want
package.json file is used for storing information about your application as
well as its dependencies. Usually, when managing your Nodejs application with
git, you should ignore the node_modules folder (which contains all the
dependencies). Instead, you can specify the library that your app use and then
let npm install it automatically for you with npm install. Here is the default
package.json file generated by express
You don’t even need to specify the dependencies manually. When you install any
new package with npm, just add --save flag so that npm will update the
package.json file automatically for you. For example
There are several ways of installing Nodejs. You can install it using your OS
packages manager or compiling it from source. However, the recommended way is to
install it using nvm. nvm can help you
install, maintain and run multiple versions of Nodejs (without sudo need).
To install nvm, simply do
Add this to your shell’s rc file
Next, install one version of nodejs that you want using nvm and set it as the default
Installing nodejs with nvm will automatically install
npm (packages manager for Node). For more information
about nvm, visit it’s homepage at
https://github.com/creationix/nvm.
To test whether nodejs works correctly, you can use the example from nodejs
page. Create a file named example.js
Currently, I’m building a website written entirely in Javascript using NodeJS
along with PostgreSQL database server. This series of tutorials is a summary of
my experience with NodeJS, PostgreSQL as well as how I solve the problems I
encountered. In this first post, I will give an outline of which frameworks,
technologies and the list of all following tutorials in this series. The reason
why I chose those technologies will be presented in the corresponding post.
2. Frameworks / Technologies
Here is the list of some main frameworks, libraries and technologies that I used
for developing the website.
Setting up ssl connection can be different for different kinds of server.
To make the configuration process easier, we can use
nginx
as a https proxy
server. In this design, the client connects to the nginx server using
https with encrypted data. After that, nginx decrypts the data and forwards
it to the real web server (also running locally in the same server with
nginx). This post demonstrates the steps for configuring nginx as an
https proxy server.
Installation
First, you need to install nginx with ssl support. On Mac OS, by default,
Macports does not install ssl for nginx, you need to use this command
Next, you need to find out the configuration file for nginx. Usually, it is
located under /etc/nginx (or /opt/local/etc/nginx for macports version).
There are already some sample configuration files with the .default extension
there for you. You can use those sample config files by removing the .default
extension.
Usually, you don’t put all the settings in nginx.conf file. Instead, you can
create another directory for storing your own ones and include them in the main
config file. For example, you put all your config files inside site-enabled
folder, add this inside the http section of the nginx.conf file.
Gulp is a task runner written in Javascript and run on
Nodejs (similar to Grunt). It helps you to automate your
daily boring, repetitive and time consuming development tasks when working with
Javascript. Some examples of those tasks are compiling LESS file to CSS,
browserify modules, compiling and uglify JS files. Simply defining those tasks
in a task file and Gulp will take care of the rest for you. This post shows how
to install and config Gulp as well as the basic usage.
Installation
You can install Gulp using Nodejs packages managing system
Also, you will need to install Gulp locally inside your project to in order to
load the module
Next, install some required Gulp plugins, I will explain about these plugins
later
Zooming functionality is a very useful feature for working with large and
complicated svg diagram. Often for those types of graph, zoom ability allows
users to have a detail view of one specific part on that graph. Fortunately,
creating zoom behavior
in d3js is an uncomplicated task since the library already
takes care of it all for you. Everything you need to do is to apply the zoom
function function on the svg element that you want. For example
In my previous post, I have demonstrated how to use aria2 as the main downloader
on Unix/Linux
(Aria2 as Default Download Manager on Unix/Linux).
However, there is still one thing missing. That is browser integration. Luckily,
Conkeror allow me to customize it very easily through the init file. To achieve,
you need to modify the content_handler_prompt function in
content-handler.js file. Add this to your .conkerorrc file for it to
overwrite the conkeror’s built in one (sadly, I haven’t found any better
solution).
Update Jun 21 2014: add another better solution that uses web-mode
1. Syntax highlighting
If you are working with Javascript, especially ReactJS, you definitely have known
about JSX, the XML syntax inside of Javascript. This section will demonstrate how
to setup Emacs for working with JSX files using web-mode, an autonomous
emacs major-mode for editing web templates. For web-mode to work properly with
JSX files, add this to your .emacs
This
is how web-mode indents and highlights my jsx files
(taken from my Emacs).
2. JSX Syntax Checking
Next, we need a tool for syntax checking. For Javascript, we have jslint (or
jshint), which I have previously written another post about how to set it up
in Emacs here
Emacs - Setup JSHint for on-the-fly (potential) errors checking.
For JSX, there is a similar tool called jsxhint. Of course, you need to
install it before you can use
By default, Sequelize (an ORM system for NodeJS) only
allows you to specify custom table name. Currently, there is no such feature in
Sequelize to help you define custom column names and then map them to properties
in model objects. To achieve this, you may need a workaround that is to use
getter and setter methods provided by Sequelize. When you creating the model,
set the property names to the same with the corresponding column name in
database first. After that, define the setter and getter functions to map those
column to the desired properties name that you want. This example transforms
model properties name from snake_case (SQL style) to camelCase (JS style).
Later, when you want to create a new instance, you can use the birthDate
instead of birth_date.