background preloader

Express - node web framework

Express - node web framework

Handle GET and POST Request in Express 4 | Code for geek As per the documentation GET request are meant to fetch data from specified resource and POST are meant to submit data to a specified resource. Express allows you to handle GET and POST request using the instance of express. Due to the depreciation of connect middle-ware handling POST request however seems confusing to many people. GET request: Handling GET request in Express seems so easy. var express = require("express");var app = express(); app.get('handle',function(request,response){//code to perform particular action. GET request can be cached and remains in browser history. POST Request: Express version 4 and above requires extra middle-ware layer to handle POST request. You can install bodyParser by two way. sudo npm install --save body-parser You have to import this package in your project and tell Express to use this as middle-ware. Once configured you can use app.post express router to handle POST request. In this way you can handle the GET and POST request in Express 4. server.js

express-redirect express-redirect offers you simple and blazing fast redirection rules. Even very complex redirect rules don't take longer than 1 ms. It just comes down to concatenating strings. Say goodbye to writing 10 lines of code for a plain redirection over and over again. If you want redirections with fancy JS expressions, you should probably check out deviate. Installation $ npm install express-redirect var express = require("express") , redirect = require("express-redirect"); var app = express(); redirect(app); Example app.redirect("/p/:id", "/page/:id"); app.redirect("/p/:id", "/page/:id", 301); app.redirect("/p/:id?" express-redirect mounts the new method app.redirect(route, target, [status], [method]) to your app. route The parameter route is a string and is required. target The parameter target is a string and is required. app.redirect("/a/:id([0-9]+)?" /a -> /b/1 /a/100 -> /b/100 /c -> /d /c/view -> /d/view status The parameter status is an integer and is optional. method License (The MIT License)

Todo App with Express.js/Node.js and MongoDB Note: This tutorial is a part of Express.js Guide: The Comprehensive Book on Express.js. Todo apps are considered to be quintessential in showcasing frameworks akin to famous Todomvc.com for front-end JavaScript frameworks. In this example, we’ll use Jade, forms, LESS, AJAX/XHR and CSRF. In our Todo app, we’ll intentionally not use Backbone.js or Angular to demonstrate how to build traditional websites with the use of forms and redirects. Example: All the source code is in the github.com/azat-co/todo-express for your convenience. Here are some screenshots of Todo app in which we start from a home page: There’s an empty list (unless you played with this app before): Now we can add four items to the Todo List: Mark one of the tasks as “done”, e.g.. Going to the Complete page reveals this done item: Deletion of an item from the Todo list is the only action performed via AJAX/XHR request. Scaffolding As usual, we start by running $ express todo-express $ cd todo-express $ npm install MongoDB app.js

Page.js by visionmedia Tiny ~1200 byte Express-inspired client-side router. page('/', index) page('/user/:user', show) page('/user/:user/edit', edit) page('/user/:user/album', album) page('/user/:user/album/sort', sort) page('*', notfound) page() Installation There are multiple ways to install page.js. $ npm install page # for browserify $ component install visionmedia/page.js $ bower install visionmedia/page.js Via CDN and script tag: Running examples To run examples do the following to install dev dependencies and run the example server: $ git clone $ cd page.js $ npm install $ node examples $ open Currently we have examples for: page(path, callback[, callback ...]) Defines a route mapping path to the given callback(s). page('/', user.list) page('/user/:id', user.load, user.show) page('/user/:id/edit', user.load, user.edit) page('*', notfound) Under certain conditions, links will be disregarded and will not be dispatched, such as: page(callback) page(path)

path - Node.js Manual Path This module contains utilities for dealing with file paths. Use require('path') to use it. It provides the following methods: path.normalize(p) Normalize a string path, taking care of '..' and '.' parts. When multiple slashes are found, they're replaces by a single one; when the path contains a trailing slash, it is preserved. Example: path.normalize('/foo/bar//baz/asdf/quux/..') path.join([path1], [path2], [...]) Join all arguments together and normalize the resulting path. node> require('path').join(... path.resolve([from ...], to) Resolves to to an absolute path. If to isn't already absolute from arguments are prepended in right to left order, until an absolute path is found. Another way to think of it is as a sequence of cd commands in a shell. path.resolve('foo/bar', '/tmp/file/', ' Is similar to: cd foo/bar cd /tmp/file/ cd .. cd a/.. The difference is that the different paths don't need to exist and may also be files. Examples: path.resolve('/foo/bar', '. path.dirname(p) path.extname(p)

visionmedia/express - GitHub

Related: