Express 4 offers a nice modular way to structure your routes.

var express = require("express"),
    router  = express.Router(),
    app     = express(),
    port    = 4000;

// Here we declare our API which will be visible under prefix path
router.get('/', function (req, res) {
    console.log("request to subspace hello");
    res.send({ message: "Hi from subspace /api/v1/"});

// we attach our routes under /api/v1
app.use('/api/v1', router);

// here we have direct, root-level routing
app.get('/', function (req, res) {
    console.log("request to rootspace hello");
    res.send({message: "Hi from root /"});

console.log("App active on localhost:" + port);

Leave a Reply

Your email address will not be published. Required fields are marked *