简体   繁体   中英

Angular JS to Node Js using Express Js

I have been trying to run a angularJs front-end with NodeJs server with expressJs. This program is merely supposed to takes user input and prints it on the server console. Having limited knowledge in JavaScript I have compiled the following codes:

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/");
});

This seems to give an error on the terminal:

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: '' }

The scene of the browser was as follows Browser Screen

The last Request in your terminal output shows it receives request for / . Your code extracts / in variable pathname . In fs.readFile you provide first argument as pathname.substr(1) which boils down to null because pathname is / here. Hence, you get the corresponding error.

@Ritik Saxena is correct. Also, you're not actually letting Express do it's job in the code you have above as you're not connecting it to the http server at all. the only code of yours actually running is the callback to the http.createServer call.

You should pass the express app to it rather than your own callback. If you want your existing callback to run you need to mount that as app middleware instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM