How to create a localhost server to run an AngularJS project

前端 未结 13 811
迷失自我
迷失自我 2020-11-29 15:56

i have used Xampp and JetBrain WebStorm to run an AngularJS project. But it\'s complicated and low performance.Is there any other way to run an AngularJS project?

13条回答
  •  青春惊慌失措
    2020-11-29 16:18

    I use:

    • express and
    • morgan

    Install Node.js. and npm. npm is installed with Node.js

    Placed inside the root project directory

    $ cd 
    

    The next command creates package.json

    $ npm init
    

    Install express ==> Fast, unopinionated, minimalist for node:

    $ npm install express --save
    

    Install morgan ==> HTTP request logger middleware for node.js

    $ npm install morgan --save
    

    create file server.js

    add the following code in server.js file

    // Required Modules
    var express    = require("express");
    var morgan     = require("morgan");
    var app        = express();
    
    var port = process.env.PORT || 3002;
    
    app.use(morgan("dev"));
    app.use(express.static("./"));
    
    app.get("/", function(req, res) {
        res.sendFile("./index.html"); //index.html file of your angularjs application
    });
    
    // Start Server
    app.listen(port, function () {
        console.log( "Express server listening on port " + port);
    });
    

    Finally run your AngularJS project in localhost server:

    $ node server.js
    

提交回复
热议问题