Using ExpressJS Router | Express API Part 2 | Hungry Turtle Code

I don’t want one long file

Let’s make more files

const express = require('express');
const router = express.Router();

module.exports router;
router.get('/test', (req, res) => {
res.send('Test route');
});
app.use(require('./routes'));
  // index.js
const express = require('express');
const app = express();

app.use(express.static('../TurtleFactQuiz'));

app.use(require('./routes'));

app.use((req, res) => {
res.status(404)
.send('Unknown Request');
});

app.listen(8080, (req, res) => {
console.log('App is listening on port 8080');
});

We have the mechanics. How about we use them

  // routes/index.js
const express = require('express');
const router = express.Router();

router.use('/api', require('./api.v0.js'));

module.exports = router;

api.v0.js

  // routes/api.v0.js
const express = require('express');
const router = express.Router();

module.exports = router;

Adding a route handler

router.get('/', (req, res) => {
res.send('API route');
});
router.use('/api', require('./api.v0.js'));
router.use('/api', require('./api.v0.js'));
router.use('/newpath', require('./api.v0.js'));

--

--

--

Lead engineer by day. Indie hacker and tinkerer by night. Occasional teacher on hungryturtlecode.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

I’ve built an angular pie-chart library in 8 hours

JavaScript Mistakes — Wrappers and Objects

Blend colors with images using Canvas

Form Validation in a Vue 3 App with Vee-Validate 4 — Validation Messages and Composition API

Formatting Relative Time with JavaScript’s RelativeTimeFormat Constructor

ReactJS mouse-move throttle

ReactJS mouse-move throttle on Codepen

Top React Hooks — Lifecycle Hooks

Getting Started with Server-Side Development with Hapi.js

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adrian Henry

Adrian Henry

Lead engineer by day. Indie hacker and tinkerer by night. Occasional teacher on hungryturtlecode.com

More from Medium

What exactly is NodeJS? Is it a good idea to use NodeJS?