Yoctol / bottender

⚡️ A framework for building conversational user interfaces.
https://bottender.js.org
MIT License
4.22k stars 335 forks source link

server/index.js requirement / typescript support #847

Open dcsan opened 4 years ago

dcsan commented 4 years ago

Describe the bug It seems bottender has a hardwired path expecting an index.js in the same directory as you run the server. I'm moving a project to typescript so the raw source is under src and I usually run in dev with ts-node

➜  server git:(move-to-ts) ✗ ts-node src/server.js
Error: Cannot find module '/Users/dc/dev/exiteer/xbot/server/index.js'
Require stack:
- /Users/dc/dev/exiteer/xbot/server/node_modules/bottender/dist/server/Server.js
- /Users/dc/dev/exiteer/xbot/server/node_modules/bottender/dist/bottender.js
- /Users/dc/dev/exiteer/xbot/server/node_modules/bottender/dist/index.js
- /Users/dc/dev/exiteer/xbot/server/src/server.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
    at Function.Module._load (internal/modules/cjs/loader.js:898:27)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)
    at DevServer.<anonymous> (/Users/dc/dev/exiteer/xbot/server/node_modules/bottender/src/server/Server.ts:51:37)
    at Generator.next (<anonymous>)
    at /Users/dc/dev/exiteer/xbot/server/node_modules/bottender/dist/server/Server.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/dc/dev/exiteer/xbot/server/node_modules/bottender/dist/server/Server.js:4:12)
    at DevServer.prepare (/Users/dc/dev/exiteer/xbot/server/node_modules/bottender/dist/server/Server.js:47:16)

I'm wondering if there's a way to pass the routes file to botTender explicitly when starting up?

src/server.js

const bot = bottender({
  dev: process.env.NODE_ENV !== 'production',
});
// the request handler of the bottender app
const handle = bot.getRequestHandler();

bot.prepare().then(() => {
  const server = express();

botRouter.js

const { router, text, slack } = require('bottender/router')
const Dispatcher = require('./mup/Dispatcher')

const botRoutes = async function App () {
  // Logger.log('init routes game', game)

  const routes = router([
    text(/^e$|^echo$/i, Dispatcher.echo),

I'm not quite sure how to wire those things together, there seems to be some lazy loading going on with the routing currently.

Expected behavior A more explicit way to startup a project, less magic requires happening behind the scenes...

This is mainly to support a project in typescript

aqzhyi commented 4 years ago

Hello, I am not sure what your essential question point is. Unfortunately, I have never used ts-node

here is my currently solution:

// project/index.js
module.exports = require('./dist/node/index.js').default

and

// project/src/server.ts
...
server.use(express.static('dist')) // to serve frontend
...
// project/src/...others.ts // and so on

currently, I use webpack { target: 'node' } and npm:webpack-node-externals to build out dist codes.

maybe, soon, https://deno.land/ could be better

marines commented 3 years ago

The point is to not hardcode file names and let users provide routing programmatically. It has little to do with ts-node.