You can do this with json-server !
How I set this up
In terminal:
npm i -D @angular-builders/custom-webpack json-server
ng config projects.<project>.architect.build.builder @angular-builders/custom-webpack:browser
ng config projects.<project>.architect.build.options.customWebpackConfig.path custom-webpack.config.js
ng config projects.<project>.architect.serve.builder @angular-builders/custom-webpack:dev-server
ng config projects.<project>.architect.serve.options.customWebpackConfig.path custom-webpack.config.js
This installs the packages needed and modifies angular.json so we can run the json server in tandem when we run ng serve
Then we need to make two files in the root of project:
- db.json
- custom-webpack.config.js
in custom-webpack.config we put
const json = require('json-server');
module.exports = {
devServer: {
before: function(app) {
app.use('/api', json.router('db.json'));
}
}
};
In db.json we can put our endpoints, in this case
{
"users": []
}
Now we can use this in our service
const API = '/api/users/';
@Injectable({
providedIn: 'root'
})
export class AppService {
constructor(private http: HttpClient) {}
get(id: number) {
return this.http.get(API + id);
}
post(user: any) {
return this.http.post(API, user);
}
}
Do a post and checkout db.json.
json-server docs: https://www.npmjs.com/package/json-server