- Ng new
- Cd into app
- Npm install —save express body-parser (express organizes an architecture to run on a node server, body parser parses json)
- Npm install —save axios (for http requests to salesforce’s jsforce api)
- Npm install —save bootstrap (for easy styling)
- Create server.js file in root folder, copy and paste this into it ` // Get dependencies const express = require('express'); const path = require('path'); const http = require('http'); const bodyParser = require('body-parser');
// Get our API routes const api = require('./server/routes/api');
const app = express();
// Parsers for POST data app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false }));
// Point static path to dist app.use(express.static(path.join(__dirname, 'dist')));
// Set our api routes app.use('/api', api);
// Catch all other routes and return the index file app.get('*', (req, res) => { res.sendFile(path.join(__dirname, 'dist/index.html')); });
/**
- Get port from environment and store in Express. */ const port = process.env.PORT || '3000'; app.set('port', port);
/**
- Create HTTP server. */ const server = http.createServer(app);
/**
- Listen on provided port, on all network interfaces.
*/
server.listen(port, () => console.log(
API running on localhost:${port}
)); ` - Create server folder
- Create routes folder in server folder
- Create a api.js file in routes folder ` const express = require('express'); const router = express.Router();
// declare axios for making http requests const axios = require('axios'); const API = 'https://jsonplaceholder.typicode.com';
/* GET api listing. */ router.get('/', (req, res) => { res.send('api works'); });
// Get all posts
router.get('/posts', (req, res) => {
// Get posts from the mock api
axios.get(${API}/posts
)
.then(posts => {
res.status(200).json(posts.data);
})
.catch(error => {
res.status(500).send(error)
});
});
module.exports = router; `
- Ng build
- Node server.js