npm i --save body-parser
"main":"index.js"
. Create a file with this name and extension.import express from 'express';
const app = express(); //run instance of express </br>
const PORT = 3000; //variable for port
app.get('/', (req, res) => //make sure we get something in return when we call URL
res.send(`Node and express server is running on ${PORT}`) //message will return on localhost 3000; template strings using ES6 syntax
);
app.listen(PORT, () =>
console.log(`your server is running on port${PORT}`)
);
"start": "nodemon ./index.js --exec babel-node -e js"
You can call a route and go to a specific page or you can also use routes to define your endpoints in an application.
routes.js
const routes = (app) => { // we are passing (app) inside of our routes function inside of our index in order to pass the endpoints that we'll just created.
//so here we're going to make several types of calls, a GET call to return all of our contacts and a POST call to add a new contact, all use same URL.
app.route('/contact')
.get((req,res) =>
res.send('GET request successful'))
.post((req,res) =>
res.send('POST request successful'));
//next route
app.route('/contact/:contactId')
.put((req,res) =>
res.send('PUT request successful'))
.delete((req,res) =>
res.send('DELETE request successful'));
//The last thing that we need to do is make sure that we export that particular function otherwise we won't be able to use it anywhere else in our application.
export default routes;
}
// import routes from routes.js
import express from 'express';
import routes from './src/routes/crmRoutes';// imported routes
//And then all we need to do at this point is to run it. So we are running the function routes and guess what, we're passing app to it. So therefore, once we run this application, we're going to pass express to it and be able to basically have those api calls available.
routes(app);//now test with Postman