Wednesday, 15 August 2012

node.js - Output JSON object in node js using express -


This is my first time using a node and I'm using the manifest profile. I am trying to output a Jason object on the "Calendar" page (when viewed in main.js). When I run the router (main.js) I get this error: Error: ENOENT, such a file or directory '/ user / macbook / desktop / node / view / (' calendar.ejs ')'

Basically I want to define my JSON object in my .html file in main.js Can someone please explain to me why the server can not find a calendar file. It can find index.ejs, but not the calendar. And is this the right way to do what I want to achieve?

I have a directory structure of setup:

  ------- node_module ------- route -------- - ---- Main.JS ------- View ------------ Calendar. Ejs ------------ Index.EJS ----- - package.json ------- server.js  

This is my server.js code:

  var express = require (' Express'); Var app = express (); Required ('./roads / main') (app); App.set ('idea', __derename + '/ view'); App.set ('visual engine', 'EJS'); App.engine ('html', Required ('ejs'). Render file); Var server = app.listen (3000, function () {console.log ("We have started our server on port server");});  

This is my main.js code:

  module.exports = function (app) {app.get ('/', function (req, Res) {res.render ('index', {title: "home page"});}); App.get ('/ calendar', function (rik, ridge) {res.json ({"foo": "bar"})}}  

Just a guess.

First you need with GET / calendar ( '/ Routes / main') (app); .

Then you overwrite GET / calendar app.set ('views', __dirname + '/ Views'); .

You have to choose whether GET / calendar returns HTML or JSON data.


No comments:

Post a Comment