Wednesday, February 24, 2016
node.js controllers
server.js
var http = require('http');
var express = require("express");
var app = express();
var controllers = require("./controllers");
app.set("view engine", "vash");
//map the routes
controllers.init(app);
app.get("/api/users", function (req, res) {
res.set("Content-Type", "application/json");
res.send({ name: "Greg", isValid: true, group: "Admin" });
});
var port = process.env.port || 3000;
var server = http.createServer(app);
server.listen(port);
Add controllers folder, add index.js, add homeControllers.js
controllers/index.js, it depends on home controller
(function (controllers) {
var homeController = require("./homeController.js");
controllers.init = function (app) {
homeController.init(app);
};
})(module.exports);
controllers/homeController.js
(function (homeController) {
homeController.init = function (app) {
app.get("/", function (req, res) {
res.render("index", { title: "Express+Vash" });
});
};
})(module.exports);
Labels:
Node.JS
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment