Setting up jsonServer in gulp with typescript: A guide

Previously, I had set up a json server and used the following code to start it: I found guidance in this GitHub repository.

Starting angular2 project with gulp

gulp-live-server.js

var gulpCore = require('gulp');
var gulpParam = require('gulp-param');
var gulpExpressServer = require('gulp-express');
var gulp = gulpParam(gulpCore, process.argv);

//server will be running on port 3100 if not explicitly specified as below
gulp.task('server', function (port,env, package) {
console.log('IN LIVVEEEEEEEEE SERVERRRRR --->>'+' port passed:' + port+ ' command line args:' + process.argv+ ' package:' +package+' env:' +env );

var expressPort = isNaN(port + '') ? 3200 : port || 3200;
var jsonServerStarter = 'server/json-server-starter.js';

gulpExpressServer.run([jsonServerStarter], { env: { expressPort: expressPort } });

    gulp.watch(['db.json'], function (event) {
        console.log('abt to notify for db.json');
        //gulpExpressServer.notify(event);
    });

});
gulp.task('default', ['server']);

Now, as I am working on an Angular2 app, I need to start the json-server using gulpfile.ts. Currently, my setup looks like this:

import * as gulp from 'gulp';
import * as runSequence from 'run-sequence';

import { PROJECT_TASKS_DIR, SEED_TASKS_DIR } from './tools/config';
import { loadTasks } from './tools/utils';


loadTasks(SEED_TASKS_DIR);
loadTasks(PROJECT_TASKS_DIR);


// --------------
// Build dev.
gulp.task('build.dev', (done: any) =>
   runSequence(//'clean.dev',
//              'tslint',
//              'css-lint',
          'build.assets.dev',
          'build.html_css',
          'build.js.dev',
          'build.index.dev',
          done));

// --------------
// Build dev watch.
gulp.task('build.dev.watch', (done: any) =>
  runSequence('build.dev',
          'watch.dev',
          done));

// --------------
// Build prod.
gulp.task('build.prod', (done: any) =>
  runSequence('clean.prod',
          'tslint',
          'css-lint',
          'build.assets.prod',
          'build.html_css',
          'copy.js.prod',
          'build.js.prod',
          'build.bundles',
          'build.bundles.app',
          'build.index.prod',
          done));   

// --------------
// Serve dev
gulp.task('serve.dev', (done: any) =>
  runSequence('build.dev',
          'server.start',
          'watch.dev',
          done)); 


// --------------
// Serve prod
gulp.task('serve.prod', (done: any) =>
  runSequence('build.prod',
          'server.prod',
          done));

//---------------
//json server
//not sure how to configure json-server here.

However, I am uncertain about how to configure the json-server in TypeScript. Any suggestions?

Answer №1

After conducting thorough research, I managed to discover a solution to this issue. Set up your project using the angular2-seed.

This allows your live-server for Angular 2 to utilize gulp, enabling you to launch your json-server by integrating it into the gulpfile.js. In this way, live-server activates gulp which in turn initiates json-server.

I am pleased to say that this approach finally proved successful for me.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method for calculating the total of a column field within an array in Angular 9.1.9?

I am using Angular 9.1.9 and Html to work with a nested array field in order to calculate the total sum and display it in a row. Within my array list ('adherant'), I am aiming to sum up a specific column's values ({{ Total Amount }}) and pr ...

Pressing the shortcut key will activate the function specified in ng-click,

I have been searching for a solution to my problem, but I haven't found anything that really helps. What I am looking for is a shortcut within an ng-click directive where there is only an if condition without an else expression. Essentially, I just wa ...

Tips for showing ng-repeat items solely when filters are applied by the user

Is there a way to only display elements when a user uses a filter? For instance: $scope.elements = [{name : 'Pablo', age : 23}, {name : 'Franco', age : 98}]; <input type="text" ng-model="searchText" /> <div ng-repeat="elemen ...

Trigger a JavaScript function after Angular has completed its process

Once all data binding is completed and there are no more tasks for the Angular javascript to perform, I need to execute a function that toggles the display of certain divs. Attempted Solutions I want to avoid using timeouts: The timing of Angular's ...

What is the best way to generate a variable amount of div elements with constantly changing content using Angular2?

I'm not entirely sure on the process, but I believe ngFor would be used in this scenario. Essentially, my goal is to generate the following div multiple times, with each iteration updating the value inside the brackets from 0 to 1, 2, and so forth... ...

Tips for including <script> tags in Angular2

Is there a way to integrate the following script into my Angular page: <script> jQuery(function () { jQuery('#camera_wrap_1').camera({ transPeriod: 500, time: 3000, height: '490px', thumbnails: ...

Deactivate a button based on a comparison condition

How can I prevent a button from being clickable when a specific condition is met? I attempted to do it this way, but it seems to be ineffective: <button type="text" disabled="{{candidature.statusCandidature.libelle == 'En cours' }}" >edit ...

Utilizing Angular.js to retrieve data from the server and automatically update at specified intervals

I attempted to create a basic app that displays the newest message from each member. Initially, it loads an array of members. Then I invoke a function called refreshMsg to iterate through the array. Within the iteration, I set a timer on it. Unfortunate ...

Tips for incorporating the observer design pattern in REST APIs (Communication between front-end and back-end)

Is it possible to subscribe once to an API and receive multiple responses until I unsubscribe from that event? If so, how can this be achieved? If not, why does this approach not align with the observer pattern's guidelines? I attempted using the yie ...

Add the $scope ng-click event to a previously hidden element once it becomes visible

If you need a clearer explanation, feel free to ask. I have incorporated a global search function into the header of my website. I am looking to show a separate input box for mobile search that utilizes the same ng-click event, but the input field remains ...

Design a dropdown menu utilizing the keys of elements in an array

Looking to create a unique custom drop down functionality where the default value is an anchor title and a list of values is displayed when clicked. Here's the structure: <div class="sort-select"> <a href="" ng-click="showList =! showList;" ...

Problem with connecting Angular data

<body ng-app="myAPP"> <div ng-controller="employeeCtrl"> <table style="border:1px solid gray"> <tr> <th>Employee Name</th> <th>Employee Address</th> <th> ...

What is the reason for receiving a Post Not Allowed error (405)?

My current tech stack includes AngularJs and Bootstrap for the front end, and Java with Spring MVC, Spring Security, and Hibernate for the backend. I followed these steps: 1. Created a user 2. Logged in 3. Filled out the profile form and when I clicked sav ...

Importing Typescript modules by specifying their namespace instead of using a function

I have been working on a project where I needed to generate typings from graphql using the gql2ts library. In the gql-2-ts file, I initially used a namespace import for glob, which resulted in TypeScript showing me an error as intended. I then switched the ...

Guide to showing a form following a button click in Angular 9

I am trying to create a feature on my page where when I click a button, a form will appear directly below it. Additionally, I would like the number of forms displayed to match the number of times the button is clicked. Being a newcomer to Angular 9, I am ...

Easy Steps to Simplify Your Code for Variable Management

I currently have 6 tabs, each with their own object. Data is being received from the server and filtered based on the tab name. var a = {} // First Tab Object var b = {} // Second Tab Object var c = {} // Third Tab Object var d = {}// Fou ...

Is it recommended to aggregate data from various tables in a REST API to create a JSON response?

I am interested in exploring various REST API standard patterns. My current implementation follows a NoSQL style, where one table contains objects (Agenda Items) each referencing records in another table (Documents). In the UI I am developing, users can se ...

Endless loop caused by Angular UI-router's promise resolving

I'm attempting to retrieve data from my SQLite database before loading a view using resolve when defining the state in ui-router. Currently, this is how I've set up the state: .state("menu", { templateUrl: "templates/menu.html", control ...

The issue arises when attempting to use the search feature in Ionic because friend.toLowerCase is not a valid function

I keep encountering an error message that says "friend.toLowerCase" is not a function when I use Ionic's search function. The unique aspect of my program is that instead of just a list of JSON items, I have a list with 5 properties per item, such as f ...

The function isSelected will always return a value of false, whereas attempting to use

let checkboxXPath = accessPolicyPage.listCheckBoxXpathS + i + accessPolicyPage.listCheckBoxXpathE; //element(by.xpath(checkboxXPath)).click(); expect(element(by.xpath(checkboxXPath)).isSelected()).toBeTruthy(); Within the code snippet above, the isSelecte ...