Giter VIP home page Giter VIP logo

now-express's Introduction

Creating a server with Express

This example offers a pre-setup project for Express that allows you to get up and running in no time!

You can run the following command now init express to fetch the example to your local machine.

This Express example features the now.json configuration file below.

{
    "version": 2,
    "name": "express",
    "builds": [
        { "src": "**/*.js", "use": "@now/node" }
    ]
}

now.json

  • The version property specifies Now 2.0.
  • The name property sets the name for the deployment.
  • The builds property allows Now to use a builder with a specific source target.

In this case we are going to use the @now/node builder to create a lambda function for every .js file in the project. There are two .js files - index.js and about/index.js, which will be made available at your Now servers' urls / and /about.

Deploy the app with Now.

$ now

Using Express with the @now/node builder

Note that we won't be creating a server, or using a listen() function to start a server, since Now is functioning as the server. Express is used just for routing and middleware purposes.

Calling express() returns a function that takes a standard Node.js http request object and http response object as parameters. Typically this function is defined as the variable app in an Node.js module, and that function also supports routing and middleware customization. And ... that function signature - (req, res) - is the exact shape that Now expects to be exported from a module. So you can return the result of the express() call, adding routing and middleware as you normally would with an express server.

Here's a very small example of using express:

const express = require('express')

const app = express()

app.get('*', (req, res) => {
    res.send(200, '<h1>Hello, world!</h1>')
})

module.exports = app

The req and res objects passed to the callback of app.get() will then be Express Request and Express Response objects, instead of Node's standard http request and response objects. So, you can use the Express res.send() method instead of the lower-level methods on the standard Node.js response object.

In the actual example code - index.js - you'll also note usage of the helmet package in the same way you'd use it in a typical express server:

app.use(helmet())

The helmet package adds extra security-related HTTP headers to HTTP responses sent from the express app.

Resources

now-express's People

Contributors

tylerreece22 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.