【问题标题】:How to run angularjs app on node server?如何在节点服务器上运行 angularjs 应用程序?
【发布时间】:2015-08-05 12:15:02
【问题描述】:

我已经制作了一个 angularjs 应用程序并在 wamp 服务器上运行它,它运行良好,但现在想将它移动到 node express 上,但是在阅读完之后,我刚刚在我的 package.json 文件中进行了更改,如下所示:

      {
        "name": "Example",
         "namelower": "example",
          "version": "0.0.1",
           "description": "An example AngularJS project",
           "readme": "README.md",
           "repository": {
           "type": "git",
           "url": "git@git.example.com:example.git"
            },
           "devDependencies": {
                        "grunt": "0.4.2",
                        "grunt-contrib-concat": "0.3.0",
"grunt-contrib-copy": "0.5.0",
"grunt-contrib-jshint": "0.8.0",
"grunt-contrib-nodeunit": "0.3.0",
"grunt-contrib-uglify": "0.2.2",
"grunt-contrib-watch": "0.5.3",
"grunt-exec": "0.4.5",
"grunt-jsdoc": "0.5.4",
"grunt-karma": "0.8.3",
"karma": "0.12.16",
"karma-jasmine": "0.1.5",
"karma-ng-html2js-preprocessor": "^0.1.2",
"karma-phantomjs-launcher": "0.1.4"

     },
         "scripts": {
          "postinstall": "bower install",
           "test": "grunt build"
                      },

           "dependencies": {
                               "protractor": "^2.1.0"
                                "express"    : "~4.7.2",
                                "mongoose"   : "~3.6.2",
                                 "morgan"     : "~1.2.2",
                                "body-parser": "~1.5.2",
                               "method-override": "~2.1.2"
                                      }
                                     }

编辑 package.json 后,我在 cmd 上运行了 npm install 但得到了这个

         error:C:\wamp\www\First-angular-App>npm install
         npm ERR! install Couldn't read dependencies
         npm ERR! Windows_NT 6.2.9200
         npm ERR! argv "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program          Files\\nod

\node_modules\npm\bin\npm-cli.js" "安装" npm 错误!节点 v0.12.7 npm 错误! npm v2.11.3 npm 错误!文件 C:\wamp\www\First-angular-App\package.json npm 错误!代码 EJSONPARSE

       npm ERR! Failed to parse json
        npm ERR! Unexpected token 'e' at 34:6
        npm ERR!     "express"    : "~4.7.2",
        npm ERR!      ^
        npm ERR! File: C:\wamp\www\First-angular-App\package.json
          npm ERR! Failed to parse package.json data.
        npm ERR! package.json must be actual JSON, not just JavaScript.
        npm ERR!
        npm ERR! This is not a bug in npm.
        npm ERR! Tell the package author to fix their package.json file.  JSON.parse

       npm ERR! Please include the following file with any support request:
       npm ERR!  C:\wamp\www\First-angular-App\npm-debug.log

我该如何做到这一点以及在 node express 上运行我的应用程序需要进行哪些其他更改??

【问题讨论】:

  • 您的package.json 好像坏了。 IE。你很可能有一些语法错误。

标签: angularjs node.js


【解决方案1】:

您在第 33 行的 "protractor": "^2.1.0" 之后缺少一个逗号。

如果您可以使用 npm init 而不是手动编写 package.json,然后使用 --save 从控制台安装所有软件包。这样可以避免拼写错误。

【讨论】:

  • 为了在 express 上运行我的应用程序我做了哪些其他更改?
  • @Hassan 只需确保您使用 bower 安装 angular 并配置 .bowerrc 以使您的路径正确。那你可能需要一个名为“body-parser”的包来访问在发布请求中传递给服务器的数据。
  • 我在此之后为模式服务器安装了 npm,但仍然无法在 nodejs 服务器上运行我的应用程序 scotch.io/tutorials/…
  • 前段时间我为自己写了一个参考指南,也许对你有帮助:smarturl.it/MEAN
【解决方案2】:

使用 Node.js 在本地服务器上部署 AngularJs 应用程序的步骤

  1. 首先在您的应用程序路径中打开cmd,然后输入npm init。提供必要的信息。

  2. 使用npm i express 安装express 并创建server.js 文件,如下所示:

    常量表达 = 要求('表达') 常量应用程序 = 快递(); 常量端口 = 3000; var path = require("path"); app.get('/',function(req,res){ res.sendFile(path.join(__dirname+'/index.html')); }); app.listen(port, () => console.log(`在端口 ${port} 上监听的示例应用程序!`))
  3. 打开http://localhost:3000/ 以查看输出。它将呈现index.html

【讨论】:

    猜你喜欢
    • 2019-10-04
    • 1970-01-01
    • 2017-01-16
    • 2018-01-19
    • 2020-02-23
    • 2019-11-03
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    相关资源
    最近更新 更多