Skip to content
This repository was archived by the owner on May 27, 2024. It is now read-only.

Canary #7

Merged
merged 12 commits into from
Aug 22, 2020
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 38 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
15 changes: 15 additions & 0 deletions custom-server.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
72 changes: 45 additions & 27 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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$/
Expand Down Expand Up @@ -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}$`)
Expand Down Expand Up @@ -4044,36 +4042,41 @@ module.exports.promise = (action, options) => {
/***/ }),

/***/ 964:
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {
/***/ (function(module, __unusedexports, __webpack_require__) {


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)
Expand All @@ -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()
}


/***/ }),

Expand Down
22 changes: 22 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ <h3 id="about">About</h3>
The Idea and Inspiration for the creation remains to be Vercel&#39;s
Next.js
</p>
<h3 id="canary-specifics-24jul-2020">Canary Specifics (24,Jul 2020)</h3>
<ul>
<li>
Ability to create a custom server instance without using the
cli.
</li>
</ul>
<pre><code class="language-js">const app = require(&#39;ftrouter&#39;)
const http = require(&#39;http&#39;)
const path = require(&#39;path&#39;)

const PORT = process.env.PORT || 3000

app({
basePath: path.join(process.cwd(), &#39;example&#39;),
}).then((appHandler) =&gt; {
http.createServer((req, res) =&gt; {
appHandler(req, res)
}).listen(PORT, () =&gt; {
console.log(&#39;Listening on, &#39; + PORT)
})
})</code></pre>
<h3 id="perks">Perks</h3>
<ul>
<li>Custom Port and Directory</li>
Expand Down
Loading