简体   繁体   English

使用Express Js将Angular JS转换为Node Js

[英]Angular JS to Node Js using Express Js

I have been trying to run a angularJs front-end with NodeJs server with expressJs. 我一直在尝试通过expressJs在NodeJs服务器上运行angularJs前端。 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: 由于对JavaScript的了解有限,因此我编写了以下代码:

Client.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 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 / . 终端输出中的最后一个Request显示它接收到/请求。 Your code extracts / in variable pathname . 您的代码提取/在变量pathname In fs.readFile you provide first argument as pathname.substr(1) which boils down to null because pathname is / here. fs.readFile您提供第一个参数作为pathname.substr(1) ,因为路径名是/ here,所以可以归结为null Hence, you get the corresponding error. 因此,您得到相应的错误。

@Ritik Saxena is correct. @Ritik Saxena是正确的。 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. 另外,由于您根本没有将Express连接到http服务器,因此实际上并没有让Express在上面的代码中完成它的工作。 the only code of yours actually running is the callback to the http.createServer call. 您实际运行的唯一代码是http.createServer调用的回调。

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. 如果要运行现有的回调,则需要将其挂载为应用中间件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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