trailsjs / trails

:evergreen_tree: Modern Web Application Framework for Node.js.
http://trailsjs.io
Other
1.66k stars 70 forks source link

Tutorial Issue, routes? #138

Closed jdell64 closed 8 years ago

jdell64 commented 8 years ago

Issue Description

I apologize if this is not the write way to report this.

Watching what I think is a video by you guys (https://www.youtube.com/watch?v=AbSp8jqFDAY) I am not able to get a response from the GET request to /api/v1/user:

# curl http://localhost:3000/api/v1/user

{
  "statusCode": 404,
  "error": "Not Found"
}

To recreate this, I created a new project, installed the latest generator-waterline (alpha-6, prior versions don't seem to work anymore), and added require('trailpack-waterline') to main.js.

The tutorial seems to autogenerate routes for the builtin User model. Am I missing something?

Environment

Edit: Here are the packages I have (noticed I put the wrong version for trails):

"dependencies": {
    "lodash": "^4.0",
    "trailpack-core": "^1.0.0-beta-3",
    "trailpack-hapi": "^1.0.0-beta-3",
    "trailpack-repl": "^1.0.0-beta-3",
    "trailpack-router": "^1.0.0-beta-4",
    "trails": "^1.0.0-beta-1",
    "trails-controller": "^1.0.0-beta-2",
    "trails-model": "^1.0.0-beta-2",
    "trails-policy": "^1.0.1",
    "trails-service": "1.0.0-beta-2",
    "waterline-sqlite3": "^1.0.0",
    "winston": "^2.1.1"
  },
jdell64 commented 8 years ago

In the 1 hour video (a talk at Michigan), Travis mentioned that you could get rid of the default info route from the routes.js file. Tried this, and got the same 404 as before.

How does trails automatically generate routes for a controller?

jaumard commented 8 years ago

@jdell64 the api change since this video, now you need to install trailpack-footprints in order to have automatic generate routes and enable footprints.

jdell64 commented 8 years ago

Thanks! how modular of you.

any good view trailpacks you can recommend?

jaumard commented 8 years ago

You're welcome :) do you mean a trailpack for views (jade, ejs...) ? There none for this because it's integrate with the webserver. All you need to do is configure config/views.js for express or for hapi look here https://github.com/trailsjs/trailpack-hapi

jdell64 commented 8 years ago

I'll take a look. This is a bit confusing for me since I came from Rails (and I was a beginner at that). The front end and back end seemed to be integrated with that. Even with Sails, there was some integration with front end.

I understand and agree with the design choices that Trails has made being so modular, but if I want to put a front-end on my api, what would you use? It sounds like I need a whole front-end server to use my back-end trails server? Sorry for such newb questions. Maybe it will help you see the learning curve for new people though?

jaumard commented 8 years ago

@jdell64 you don't need another server at all for your front end. Trails can be use to serve views/assets as well as backend API. If you need a TaskRunner to build your assets (SASS/LESS, Typescript, uglify...) you have currently 2 choices : Webpack or Gulp (https://github.com/trailsjs/trailpack-webpack or https://github.com/YannBertrand/trailpack-gulp).

To choose your template engine for your views (Jade; EJS...), it's what my earlier message explain, express and hapi can serve views if you configure the template engine under config/views.js.

Sorry if I'm not clear my English is sometime limited :) hope it's better now, don't hesitate to ask questions. I will try to make an example that use basic views and give you the link here.

jdell64 commented 8 years ago

Your English is fine, I appreciate your help. I look forward to your example.

jdell64 commented 8 years ago

Just got the new beta-1 update... LOVE the question of do you want ORM and do you want footprints. reminds me of https://github.com/RailsApps/rails-composer

Does throw an error on my machine though (brand new app, yo trails, yes to ORM, yes to footprints):


debug: The config setting i18n.lng is defined by both trailpack-core and the application. The lng config defined in trailpack-core will be ignored.
debug: The config setting i18n.resources is defined by both trailpack-core and the application. The resources config defined in trailpack-core will be ignored.
debug: The config setting resources.en is defined by both trailpack-core and the application. The en config defined in trailpack-core will be ignored.
debug: trails event: trailpack:core:constructed
debug: trails event: trailpack:repl:constructed
debug: trails event: trailpack:router:constructed
debug: trails event: trailpack:hapi:constructed
debug: trails event: trailpack:waterline:constructed
debug: trails event: trailpack:footprints:constructed
debug: trails event: trails:start
debug: trails event: trailpack:footprints:validated
debug: trails event: trailpack:repl:validated
debug: trails event: trailpack:core:validated
debug: trails event: trailpack:waterline:validated
debug: trails event: trailpack:router:validated
error: 
 TypeError: config.web: ValidationError: child "port" fails because ["port" is required]
    at /home/jeff/dev/trails/sample_app/node_modules/trailpack-hapi/lib/validator.js:9:32
    at internals.Any.applyFunctionToChildren.internals.Any._validateWithOptions (/home/jeff/dev/trails/sample_app/node_modules/trailpack-hapi/node_modules/joi/lib/any.js:648:16)
    at root.validate (/home/jeff/dev/trails/sample_app/node_modules/trailpack-hapi/node_modules/joi/lib/index.js:103:23)
    at /home/jeff/dev/trails/sample_app/node_modules/trailpack-hapi/lib/validator.js:8:11
    at Object.validateWebConfig (/home/jeff/dev/trails/sample_app/node_modules/trailpack-hapi/lib/validator.js:7:12)
    at Hapi.validate (/home/jeff/dev/trails/sample_app/node_modules/trailpack-hapi/index.js:27:21)
    at /home/jeff/dev/trails/sample_app/node_modules/trails/lib/trailpack.js:61:26
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)
    at Function.Module.runMain (module.js:449:11)
    at startup (node.js:146:18)
    at node.js:404:3
error: 
 The application attempted to shut down, but is not in a started state. Either it is in the process of shutting down, or did not start successfully. Trails will not attempt to shut down twice.
error: 
 Try increasing the loglevel to "debug" to learn more
jaumard commented 8 years ago

@jdell64 I just see I already to one example here https://github.com/jaumard/trails-example-express-gulp :) dependencies are not up to date but it can be a start and when I can I'll update it.

For your error do you have a config/web.js file ? With an attribut port

jaumard commented 8 years ago

@jdell64 I reproduce the problem, you have to add config/web.js under config/index.js, it's a bug I will add it on the right repo. Thanks

jdell64 commented 8 years ago

oh sorry, i reported it under a new bug (149)

jdell64 commented 8 years ago

If you build a new app, create the web.js file (confirmed it is not created currently), add the require line to config/index.js, and npm start the application, you make calls to the api/v1/default/info path and get good responses:

{
  "app": "0.0.0",
  "node": "v5.10.1",
  "libs": {
    "http_parser": "2.6.2",
    "node": "5.10.1",
    "v8": "4.6.85.31",
    "uv": "1.8.0",
    "zlib": "1.2.8",
    "ares": "1.10.1-DEV",
    "modules": "47",
    "openssl": "1.0.2g"
  },
  "trailpacks": [
    {
      "name": "core",
      "version": "1.0.0-beta-6"
    },
    {
      "name": "repl",
      "version": "1.0.0-beta-4"
    },
    {
      "name": "router",
      "version": "1.0.0-beta-4"
    },
    {
      "name": "hapi",
      "version": "1.0.0-beta-3"
    },
    {
      "name": "waterline",
      "version": "1.0.0-alpha-6"
    },
    {
      "name": "footprints",
      "version": "1.0.0-beta-6"
    }
  ]
}

You cannot, however, make a request of /api/v1/user and get a good response (500 status code):

debug: [FootprintController] (findAssociation) api v1 -> user undefined , criteria =
Debug: internal, implementation, error 
    TypeError: Uncaught error: Cannot read property 'attributes' of undefined
    at FootprintService.findAssociation (/home/jeff/dev/trails/sample_app/node_modules/trailpack-waterline/api/services/FootprintService.js:165:39)
    at FootprintController.findAssociation (/home/jeff/dev/trails/sample_app/node_modules/trailpack-hapi/api/controllers/FootprintController.js:139:30)
    at Object.exports.execute.internals.prerequisites.internals.handler.callback [as handler] (/home/jeff/dev/trails/sample_app/node_modules/hapi/lib/handler.js:96:36)
    at /home/jeff/dev/trails/sample_app/node_modules/hapi/lib/handler.js:63:26
    at done (/home/jeff/dev/trails/sample_app/node_modules/hapi/node_modules/items/lib/index.js:31:25)
    at done (/home/jeff/dev/trails/sample_app/node_modules/hapi/node_modules/items/lib/index.js:63:25)
    at /home/jeff/dev/trails/sample_app/node_modules/hapi/lib/handler.js:49:28
    at finalize (/home/jeff/dev/trails/sample_app/node_modules/hapi/lib/handler.js:281:20)
    at wrapped (/home/jeff/dev/trails/sample_app/node_modules/hapi/node_modules/hoek/lib/index.js:867:20)
    at internals.Response._prepare.internals.Response._processPrepare.next [as _processPrepare] (/home/jeff/dev/trails/sample_app/node_modules/hapi/lib/response.js:448:16)
    at internals.Response._prepare.next [as _prepare] (/home/jeff/dev/trails/sample_app/node_modules/hapi/lib/response.js:387:21)
    at /home/jeff/dev/trails/sample_app/node_modules/hapi/lib/reply.js:151:22
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)
jaumard commented 8 years ago

@jdell64 you're right I open two bug for this https://github.com/trailsjs/trailpack-hapi/issues/76 and here https://github.com/trailsjs/generator-trails/issues/37 to quick fix this footprints.js need to also be added under index.js

jdell64 commented 8 years ago

Thanks! It looks like parentModel is undefined. Should the parentModelName be 'api'?

jdell64 commented 8 years ago

I may have figured it out... it is ignoring the prefix now (not sure if intended).

http://localhost:3000/user works

http://localhost:3000/api/v1/user (the oldway) does not work

jaumard commented 8 years ago

Normally this bug was solved check generator install the last version of trailpack-footprints if it's the case don't hesitate to create a new issue on trailpack-footprints repo. Check the prefix was set to /api/v1/ under config/footprints.js too :)

jdell64 commented 8 years ago

I have the most recent version. Created a new bug.

jaumard commented 8 years ago

All have to work now :)

jaumard commented 8 years ago

@wbprice this can be close