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 @@

About

The Idea and Inspiration for the creation remains to be Vercel's Next.js

+

Canary Specifics (24,Jul 2020)

+ +
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