Ich habe versucht, ein angularJs Front-End mit NodeJs Server mit ExpressJs zu betreiben. Dieses Programm soll lediglich Benutzereingaben verarbeiten und auf der Serverkonsole ausgeben. Mit begrenzte Kenntnisse in JavaScript habe ich die folgenden Codes zusammengestellt:Angular JS zu Knoten Js mit Express Js
Client.js
angular.module("appModule",[]).
controller("appController", function($scope,$http){
$scope.action = function() {
console.log($scope.data);
var post = $http({
method: 'POST',
url: '/Data',
data: $scope.mod,
processData: false
})
post.success(function (response) {
console.log(response);
$scope.response.data = response;
});
}
});
Server.js
var express = require('express');
var fs = require("fs");
var url = require("url")
var http = require('http');
var path = require('path');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/Data', function (req, res) {
console.log(req.body);
res.setHeader('Content-Type', 'application/json');
req.body.serverMessage = "NodeJS replying to angular"
res.end(JSON.stringify(req.body));
});
http.createServer(function(request,response){
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
fs.readFile(pathname.substr(1),function (err,data){
if(err){
console.log(err);
response.writeHead(404, {'Content-Type': 'text/html'});
}else{
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data.toString());
}
response.end();
});
}).listen(8081, function() {
console.log("Server running at http://127.0.0.1:8081/");
});
Dies scheint einen Fehler auf dem Terminal zu geben:
Server running at http://127.0.0.1:8081/
Request for /public/WebPageView.html received.
Request for /public/JavaScriptCode.js received.
Request for/received.
{ Error: ENOENT: no such file or directory, open ''
at Error (native) errno: -2, code: 'ENOENT', syscall: 'open', path: '' }
Die Szene des Browsers war wie folgt Browser Screen
Können Sie mir bitte sagen, wie soll ich das machen? –
Vielen Dank, es hat funktioniert !! –