1

I recently abandoned the awesome LAMP solution for Node/AngularJS and I have some serious (and noob) difficulties to begin. I took an existing AngularJS project based on Angular Seed and I didn't figure out how can I add some backend javascript code.

In online tutorials, I always find an app.js file, in which there is some "requires" and where I can add an extra server code. To launch this kind of project I have to node app.js

In my Angular-seed based project, there is only a package.json that contains script commands. Also, I noticed that to launch it, I have to npm start.

Where can I put my NodeJS code in this project ?

Thanks in advance !

1
  • You can put your code anywhere you like, npm start is just a shorthand for smth like npm app.js or npm server.js Commented Jul 27, 2017 at 9:26

1 Answer 1

1

Usually you will have two differents projects. The backend (Nodejs) and the frontend (Angular). You can expose your backend logic using a public API that your frontend will use. For example you can expose a REST API using nodejs with help of express. I recommend you to take a look at swagger that can help you to define your api.

After that using your angular app you can send different requests to that API and consume the info that receive from it.

To sum up you will have two different projects. Hope this helps

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply ! Actually, that's exactly what I want. However, if I start two projects, I necessarily have to make them listen two different ports (in which case I will have a Same origin policy problem for the API calls, but for some reasons I'm not allowed to Access-Control-Allow-Origin). Is there a way to have two projects listening at the same port ?
@J.Doe If you want to run the project on same port, create node js file in your angular project as server.js
You can create the nodejs file in your angular project as @k11k2 said. Or you can allow CORS in your project from known addresses and hosts
Thanks to both of you ! However, if I create a server.js file, I don't know how to run it in the same context then the project. The script line start in package.json contains this http-server -a localhost -p 4000 -c-1 ./app (Also, I'm not allowed to use the CORS option in my project)
That will be a different question. Open a new one for that. Try using scripts inside your package.json that you can invoke with npm. Or you can use gulp tasks for that
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.