So I'm hitting my head against the wall with this:
SERVER.JS
const express = require('express');
const app = express();
//const app = require('express')();
const cors = require('cors')
const bodyParser = require('body-parser');
app.use(express.static(__dirname + '/public'));
//Enable CORS
app.use(cors());
const polls = [
{
...
}
];
//Enable CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(bodyParser.json());
app.get('/polls', (req, res) => {
let result = {polls:polls.map(function(p){
return {id: p.id, title: p.title};
})};
res.json(result);
});
app.get('/polls/:id', (req, res) => {
let id = req.params.id-1;
res.json(polls[id]);
});
const port = process.env.PORT ? process.env.PORT : 8081;
const server = app.listen(port, () => {
console.log("Server listening port %s", port);
});
So why isn't the part app.use(express.static(...)) not working? I've tried something like this:
CONTROLLE.JS
var app = angular.module('app',[]);
app.controller('AppCtrl',['$scope','$http',function($scope,$http) {
console.log("hello");
var polls = function() {
$http.get('/polls').then(function(response) {
$scope.result = response;
});
};
}]);
index.html
<!DOCTYPE>
<html>
<head>
<title>myapp</title>
<meta
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script type="text/javascript" src="../server.js"></script>
<script type="text/javascript" src="controller.js"></script>
<script>
</script>
</head>
<body>
<div ng-app="app" ng-controller="AppCtrl">
</div>
</body>
</html>
But I can't even get the console.log("hello") out. Any help? I would appreciate if someone would tell me how to do this without writing the app.use(express.static...) in the server.js. Or is it even possible to get the result-map out without adding the express.static (or anything else to the server.js)?
And how can I get this working by just writing node server.js to my git bash?
server. ?app.listen(3000, () => console.log('Example app listening on port 3000!'))