diff --git a/README.md b/README.md index 7367420..3ddec2a 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,28 @@ ftrouter started as a clone of the Next.js' Api Routes implmentation and is now The Idea and Inspiration for the creation remains to be Vercel's Next.js +### Canary Specifics (24,Jul 2020) + +- Ability to create a custom server instance without using the cli. + +```js +const app = require('ftrouter') +const http = require('http') +const path = require('path') + +const PORT = process.env.PORT || 3000 + +app({ + basePath: path.join(process.cwd(), 'example'), +}).then((appHandler) => { + http.createServer((req, res) => { + appHandler(req, res) + }).listen(PORT, () => { + console.log('Listening on, ' + PORT) + }) +}) +``` + ### Perks - Custom Port and Directory diff --git a/app.js b/app.js index e58b517..0f1b9ee 100644 --- a/app.js +++ b/app.js @@ -5,28 +5,33 @@ const http = require('http') const microServer = require('./lib/micro-server') const setupRoutes = require('./lib/setup-routes') const warn = require('./lib/warn') +const basePath = require('./lib/base-path') -const argv = require('minimist')(process.argv.slice(2)) -const port = argv.p || argv.port || 3000 +function main() { + const argv = require('minimist')(process.argv.slice(2)) + const port = argv.p || argv.port || 3000 -if (process.argv[1].includes('routex')) { - warn( - 'routex has been renamed/replaced by ftrouter, You can fix it by renaming your executables to ftrouter.' - ) -} + if (process.argv[1].includes('routex')) { + warn( + 'routex has been renamed/replaced by ftrouter, You can fix it by renaming your executables to ftrouter.' + ) + } -setupRoutes() - .then((availableRoutes) => { - http.createServer((req, res) => { - microServer(req, res, availableRoutes) - }).listen(port, () => { - console.log('> Listening on ' + port) - }) - }) - .catch((err) => { - console.log(err) - throw err + setupRoutes({ + basePath: basePath(), }) + .then((availableRoutes) => { + http.createServer((req, res) => { + microServer(req, res, availableRoutes) + }).listen(port, () => { + console.log('> Listening on ' + port) + }) + }) + .catch((err) => { + console.log(err) + throw err + }) +} process.on('uncaughtException', (err) => { console.error(err) @@ -37,3 +42,18 @@ process.on('unhandledRejection', (err) => { console.error(err) throw err }) + +function expose() { + return (config) => { + return setupRoutes(config).then((availableRoutes) => { + const handler = (req, res) => microServer(req, res, availableRoutes) + return handler + }) + } +} + +if (require.main == module) { + main() +} else { + module.exports = expose() +} diff --git a/custom-server.js b/custom-server.js new file mode 100644 index 0000000..30cf77c --- /dev/null +++ b/custom-server.js @@ -0,0 +1,15 @@ +const app = require('./dist/index') +const http = require('http') +const path = require('path') + +const PORT = process.env.PORT || 3000 + +app({ + basePath: path.join(process.cwd(), 'example'), +}).then((appHandler) => { + http.createServer((req, res) => { + appHandler(req, res) + }).listen(PORT, () => { + console.log('Listening on, ' + PORT) + }) +}) diff --git a/dist/index.js b/dist/index.js index 60f4b8d..d991ee8 100755 --- a/dist/index.js +++ b/dist/index.js @@ -573,13 +573,10 @@ function isNumber (x) { /***/ 116: /***/ (function(module, __unusedexports, __webpack_require__) { -const basePath = __webpack_require__(973) -const path = __webpack_require__(622) const processDirectories = __webpack_require__(239) -module.exports = () => { - const processingPath = path.join(basePath()) - return processDirectories(processingPath) +module.exports = (config) => { + return processDirectories(config.basePath) } @@ -742,13 +739,14 @@ MuteStream.prototype.close = proxy('close') const fs = __webpack_require__(747).promises const path = __webpack_require__(622) -const basePath = __webpack_require__(973) const { createRouteParser } = __webpack_require__(854) let mainRouterTree = {} +let processingDir module.exports = async (directory) => { try { + processingDir = directory await processDirectory(directory, '.') return mainRouterTree } catch (err) { @@ -789,7 +787,7 @@ async function processDirectory(currPath, dir) { } function processFile(file, filePath) { - const _basePath = basePath() + const _basePath = processingDir const ignoredPath = filePath.replace(_basePath, '') const paramRegex = /^\[(\w+)\].js$/ @@ -3441,7 +3439,7 @@ function isDynamicRoute(route) { const dynRegex = /(\[\w+\])/g const matchGroups = route.match(dynRegex) || [] matchGroups.forEach((groupItem) => { - routeString = routeString.replace(groupItem, '(\\w+)') + routeString = routeString.replace(groupItem, '((\\w+[-]*)+)') }) routeString = routeString.replace(/\//g, '\\/') const parser = RegExp(`^${routeString}$`) @@ -4044,7 +4042,7 @@ module.exports.promise = (action, options) => { /***/ }), /***/ 964: -/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) { +/***/ (function(module, __unusedexports, __webpack_require__) { const http = __webpack_require__(605) @@ -4052,28 +4050,33 @@ const http = __webpack_require__(605) const microServer = __webpack_require__(544) const setupRoutes = __webpack_require__(116) const warn = __webpack_require__(539) +const basePath = __webpack_require__(973) -const argv = __webpack_require__(109)(process.argv.slice(2)) -const port = argv.p || argv.port || 3000 +function main() { + const argv = __webpack_require__(109)(process.argv.slice(2)) + const port = argv.p || argv.port || 3000 -if (process.argv[1].includes('routex')) { - warn( - 'routex has been renamed/replaced by ftrouter, You can fix it by renaming your executables to ftrouter.' - ) -} + if (process.argv[1].includes('routex')) { + warn( + 'routex has been renamed/replaced by ftrouter, You can fix it by renaming your executables to ftrouter.' + ) + } -setupRoutes() - .then((availableRoutes) => { - http.createServer((req, res) => { - microServer(req, res, availableRoutes) - }).listen(port, () => { - console.log('> Listening on ' + port) - }) - }) - .catch((err) => { - console.log(err) - throw err + setupRoutes({ + basePath: basePath(), }) + .then((availableRoutes) => { + http.createServer((req, res) => { + microServer(req, res, availableRoutes) + }).listen(port, () => { + console.log('> Listening on ' + port) + }) + }) + .catch((err) => { + console.log(err) + throw err + }) +} process.on('uncaughtException', (err) => { console.error(err) @@ -4085,6 +4088,21 @@ process.on('unhandledRejection', (err) => { throw err }) +function expose() { + return (config) => { + return setupRoutes(config).then((availableRoutes) => { + const handler = (req, res) => microServer(req, res, availableRoutes) + return handler + }) + } +} + +if (require.main == require.cache[eval('__filename')]) { + main() +} else { + module.exports = expose() +} + /***/ }), diff --git a/docs/index.html b/docs/index.html index 4233b0a..60862ad 100644 --- a/docs/index.html +++ b/docs/index.html @@ -41,6 +41,28 @@
const app = require('ftrouter')
+const http = require('http')
+const path = require('path')
+
+const PORT = process.env.PORT || 3000
+
+app({
+ basePath: path.join(process.cwd(), 'example'),
+}).then((appHandler) => {
+ http.createServer((req, res) => {
+ appHandler(req, res)
+ }).listen(PORT, () => {
+ console.log('Listening on, ' + PORT)
+ })
+})
A minimal file tree based api router for node rest api's.
+ + + + + + ++ A minimal file tree based api router for node rest + api's. +
- + - - -
-
-
A minimal file tree based api router for building rest api's with node
-ftrouter started as a clone of the Next.js' Api Routes implmentation and is now on it's path to compete with other frameworks as the simplest way to setup API routes. There's been numerous posts on why using the folder tree makes it atomic and easier to handle the separation between logic. While you cannot bundle code with ftrouter since each file is independent of the other and doesn't need the others for its execution.
-The Idea and Inspiration for the creation remains to be Vercel's Next.js
-Screenshot of autocannon
to benchmark /api
from the examples folder
This library is still in active development and is bound to have bugs , kindly make sure you use it only for testing and not for production as of now.
-# for global install to avoid installing the devDependencies
+
+
+
+ A minimal file tree based api router for building rest api's + with node +
++ ftrouter started as a clone of the Next.js' Api Routes + implmentation and is now on it's path to compete with other + frameworks as the simplest way to setup API routes. There's + been numerous posts on why using the folder tree makes it + atomic and easier to handle the separation between logic. + While you cannot bundle code with ftrouter since each file + is independent of the other and doesn't need the others for + its execution. +
++ The Idea and Inspiration for the creation remains to be + Vercel's Next.js +
+
+ Screenshot of autocannon
to benchmark
+ /api
from the examples folder
+
+
+
+ This library is still in active development and is bound to + have bugs , kindly make sure you use it only for testing and + not for production as of now. +
+# for global install to avoid installing the devDependencies
npm i -g barelyhuman/ftrouter --only=prod
# for local install to avoid installing the devDependencies
npm i barelyhuman/ftrouter --only=prod
-
# for global install to avoid installing the devDependencies
+
+ # for global install to avoid installing the devDependencies
npm i -g barelyhuman/ftrouter#canary --only=prod
# for local install to avoid installing the devDependencies
-npm i barelyhuman/ftrouter#canary --only=prod
You can run ftrouter
in any folder and the .js
files will be considered as routes.
-The CLI considers the api
folder to be the root and will pass down http req,res
to the exported function.
Then go ahead and create directories and files under any folder as mentioned or check the examples
folder for reference.
Example file tree:
-We create a folder example
you might want to call it something like routes
and point ftrouter
to it using -d ./routes
to give you an http Server running for the files inside of the routes
folder.
-example
+npm i barelyhuman/ftrouter#canary --only=prod
+
+ You can run ftrouter
in any folder and the
+ .js
files will be considered as routes. The CLI
+ considers the api
folder to be the root and
+ will pass down http req,res
to the exported
+ function.
+
+ Then go ahead and create directories and files under any
+ folder as mentioned or check the
+ examples
folder for reference.
+
Example file tree:
+
+ We create a folder example
you might want to
+ call it something like routes
and point
+ ftrouter
to it using
+ -d ./routes
to give you an http Server running
+ for the files inside of the routes
folder.
+
-example
- api
- me.js // this compiles to <host>:<port>/api/me
- [id].js; // this compile to <host>:<port>/api/<dynamicParameterId>
-Example me.js
that only handles GET
requests:
module.exports = (req, res) => {
+
+ Example me.js
that only handles
+ GET
requests:
+
+ module.exports = (req, res) => {
if(req.method === 'GET'){
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Hello World!');
@@ -97,40 +262,79 @@
-Example [id].js
that handles the dynamic path param:
-GET|POST|DELETE /api/1
-module.exports = (req, res) => {
+
+ Example [id].js
that handles the dynamic path
+ param:
+
+ GET|POST|DELETE /api/1
+ module.exports = (req, res) => {
res.write('path param ' + JSON.stringify(req.params)) // {"id":1};
res.end();
};
-Then run ftrouter on the root folder of the project, this folder should contain the api
folder or specify a directory using the -d
or --dir
command.
-# If installed globally
+
+ Then run ftrouter on the root folder of the project, this
+ folder should contain the api
folder or specify
+ a directory using the -d
or
+ --dir
command.
+
+
+ # If installed globally
ftrouter -d ./example
# If installed locally
npx ftrouter -d ./example
-
-
-
- CLI Commands
--
-
-d | --dir
to specify the directory to be used for routes, defaults to api
-
--
-
-p | --port
to specify the port to start the http server on , defaults to 3000
-
-
-Example, the following would use the example
folder as the base path for the routes and start the server on port 3001
- ftrouter -d ./example -p 3001
-
-
-
-
-
-
-
+
+
-d | --dir
to specify the directory to be
+ used for routes, defaults to api
+ -p | --port
to specify the port to start
+ the http server on , defaults to 3000
+
+ Example, the following would use the
+ example
folder as the base path for the routes
+ and start the server on port 3001
+
+ ftrouter -d ./example -p 3001
+
+
-
-
A minimal file tree based api router for building rest api's with node
- -### About - -RouteX started as a clone of the Next.js' Api Routes implmentation and is now on it's path to compete with other frameworks as the simplest way to setup API routes. There's been numerous posts on why using the folder tree makes it atomic and easier to handle the separation between logic. While you cannot bundle code with routex since each file is independent of the other and doesn't need the others for its execution. - -The Idea and Inspiration for the creation remains to be Vercel's Next.js - -### Perks - -- Custom Port and Directory -- Minimal so can be used with any bundler or process handler. -- [Performance](#Performance) focused -- Pre-compiled state for handling route requests - -### Performance - -Screenshot of `autocannon` to benchmark `/api` from the examples folder - - - -### Warning - -This library is still in active development and is bound to have bugs , kindly make sure you use it only for testing and not for production as of now. - -### Installation - -#### Stable Cli - -```sh -# for global install to avoid installing the devDependencies -npm i -g barelyhuman/routex --only=prod -# for local install to avoid installing the devDependencies -npm i barelyhuman/routex --only=prod - -``` - -#### Canary Cli - -```sh -# for global install to avoid installing the devDependencies -npm i -g barelyhuman/routex#canary --only=prod -# for local install to avoid installing the devDependencies -npm i barelyhuman/routex#canary --only=prod -``` - -### Usage - -You can run `routex` in any folder and the `.js` files will be considered as routes. -The CLI considers the `api` folder to be the root and will pass down http `req,res` to the exported function. - -Then go ahead and create directories and files under any folder as mentioned or check the `examples` folder for reference. - -Example file tree: - -We create a folder `example` you might want to call it something like `routes` and point `routex` to it using `-d ./routes` to give you an http Server running for the files inside of the `routes` folder. - -``` --example - - api - - me.js // this compiles to
+
+
A minimal file tree based api router for building rest api's with node
+ +### About + +RouteX started as a clone of the Next.js' Api Routes implmentation and is now on it's path to compete with other frameworks as the simplest way to setup API routes. There's been numerous posts on why using the folder tree makes it atomic and easier to handle the separation between logic. While you cannot bundle code with routex since each file is independent of the other and doesn't need the others for its execution. + +The Idea and Inspiration for the creation remains to be Vercel's Next.js + +### Perks + +- Custom Port and Directory +- Minimal so can be used with any bundler or process handler. +- [Performance](#Performance) focused +- Pre-compiled state for handling route requests + +### Performance + +Screenshot of `autocannon` to benchmark `/api` from the examples folder + + + +### Warning + +This library is still in active development and is bound to have bugs , kindly make sure you use it only for testing and not for production as of now. + +### Installation + +#### Stable Cli + +```sh +# for global install to avoid installing the devDependencies +npm i -g barelyhuman/routex --only=prod +# for local install to avoid installing the devDependencies +npm i barelyhuman/routex --only=prod + +``` + +#### Canary Cli + +```sh +# for global install to avoid installing the devDependencies +npm i -g barelyhuman/routex#canary --only=prod +# for local install to avoid installing the devDependencies +npm i barelyhuman/routex#canary --only=prod +``` + +### Usage + +You can run `routex` in any folder and the `.js` files will be considered as routes. +The CLI considers the `api` folder to be the root and will pass down http `req,res` to the exported function. + +Then go ahead and create directories and files under any folder as mentioned or check the `examples` folder for reference. + +Example file tree: + +We create a folder `example` you might want to call it something like `routes` and point `routex` to it using `-d ./routes` to give you an http Server running for the files inside of the `routes` folder. + +``` +-example + - api + - me.js // this compiles to