简体   繁体   中英

NodeJS/AngularJS - What steps do I need?

I have my AngularJS project working on my local machine and I'm thinking of playing with NodeJS and try to see how it will come out and I have watched lot of videos and read on blog but I did not come across plain straight forward what steps I need in order to publish my AngularJS project in NodeJS.

If you guys have already done this process will you be able to point me or show what I need to do?

As I said, I have my AngularJS project up and running and I'm planning to publish my AngularJS eventually to Heroku at later time.

So if I understand correctly you want to serve your AngularJS application on Heroku using a NodeJS server.

If that's correct you're pretty close as you have done the hardest part : your application. =)

NodeJS here we come!

First you need to initiate your node project.

npm init

This will generate a package.json file that will contain a description of your project and its dependencies.

Serving static files with Express is quite easy all you need to do is install it :

npm install --save express

Write a js file named app.js containing the following lines :

var express = require('express');
var app = express();

// webapp is the folder containg your application
app.use('/', express.static('webapp'));

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Example app listening at http://%s:%s', host, port);
});

Place your application in a folder (named webapp in my example but you cat change that if you want to).

Then you can run

node app.js

Your app is now running locally on a NodeJS server at http://locahost:3000

For the Heroku part looking briefly at their doc makes me think that it will try to run

npm start

To put it simply npm allows you to define sort of aliases in the scripts section of your package.json file. Note that test and start are special as you can run them directly using npm start / npm test. You can use any word you want but if you name it server for example the call will be npm run server (and not just npm server).

Add this to your package.json to make 'npm start' run 'node app.js' :

"scripts": {
  "start": "node app.js"
}

From there I think you're good to go live!

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