I'm new to Express and I'm trying to convert this project
https://github.com/shawn-simon-developer/NodeAngularProxyhttps://github.com/shawn-simon-developer/NodeAngularProxy into one where I can use Jade. I've installed Jade via npm. My folder structure looks like this:
myProject
--app
----css (inside e.g. bootstrap)
----index.jade
----js
------controllers
------services
----libs
----templates
------main.jade
--index.js
--node_modules
----(tons of modules in here e.g.)
----jade
----sass
----express
--package.json
index.js looks like this:
/**
* Module dependencies.
*/
var express = require('express');
var jade = require('jade');
var httpProxy = require('http-proxy');
var bodyParser = require('body-parser');
var apiForwardingUrl = 'http://api.open-notify.org/astros.json?';
// Solution for forwarding from http to https taken from:
// http://stackoverflow.com/questions/15801014/how-to-use-node-http-proxy-for-http-to-https-routing
var proxyOptions = {
changeOrigin: true
};
httpProxy.prototype.onError = function (err) {
console.log(err);
};
var apiProxy = httpProxy.createProxyServer(proxyOptions);
console.log('Forwarding API requests to ' + apiForwardingUrl);
// Node express server setup.
var server = express();
server.set('port', 3000);
server.use(express.static(__dirname + '/app'));
server.all("/space/*", function(req, res) {
apiProxy.web(req, res, {target: apiForwardingUrl});
});
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({
extended: true
}));
// Start Server.
server.listen(server.get('port'), function() {
console.log('Express server listening on port ' + server.get('port'));
});
server.set('view engine', 'jade'); // register the template engine
server.get('/templates/:name', function (req, res)
{ var name = req.params.name;
res.render('templates/' + name);
});
And then index.jade like this
html
head
title Angular App
// Angular Modules
script(type='text/javascript', src='libs/angular/angular.js')
script(type='text/javascript', src='libs/angular-route/angular-route.js')
script(type='text/javascript', src='libs/jquery/jquery.js')
// CSS Modules
link(rel='stylesheet', type='text/css', href='css/bootstrap/bootstrap.min.css')
body(ng-app='app')
.container(ng-view='')
// JS Modules
script(type='text/javascript', src='js/app.js')
script(type='text/javascript', src='js/controllers/MainCtrl.js')
script(type='text/javascript', src='js/services/apiService.js')
and finally main.jade
{{ctrl.test}}
p {{ctrl.wikiData | json}}
h1 test
that's it. I can start the server using node index.js
in the root of the project. however when I go to my localhost:3000
I get the Cannot GET /
issue that many others also seem to have. However, since I'm new to Node and Express I haven't been able to generalise the answers on other questions and solve my own.
Could anyone help me in what's going wrong here?
thanks already!
EDIT 1: I have added
server.set('views', __dirname + '/app/templates');
server.get('/', function (req, res) { res.render('index', {}); });
server.get('/', function (req, res) {
res.send('hello');
});
to the end of index.js and moved the index.jade file inside the templates folder. Now what I'm getting is a log to the console like this:
angular.js:10506 GET http://localhost:3000/templates/main 404 (Not Found)
That's one step further but not a solution yet