lite-server
lite-server copied to clipboard
Use lite-server to request a local json file.
A descriptive title
I use lite-server
and want to request a local json file books.json
.
But it give me an error: ERROR Error: Response with status: 404 Not Found for URL: ./app/mock/books.json
bs-config.json
:
{
"server": {
"baseDir": "src",
"routes": {
"/node_modules": "node_modules"
}
}
}
my component:
export class ImpureAndCachingComponent {
public bookUrl: string = './app/mock/books.json';
}
my directory structure:
project
- src
- app
- directives
- components
- MyComponent/index.ts <- the bookUrl is here
- mock
- books.json <- I want to request the json file
- pipes
- services
- index.html
- main.ts
- styles.css
I set the bookUrl
relative the component file and relative the bs-config.json
baseDir
, both of them do not work, give me 404
.
How can I use lite-server
to request local json file? Is it support?
Environment
-
lite-server
version: ^2.2.2 -
nodejs
version: v6.6.0 -
npm
version: 4.1.2 - OS type/version: 10.11.6
Have you tried making an ajax request to it?
private bookUrl: string = '../../mock/books.json';
private books: Book[];
constructor(http: Http) {
http.get(bookUrl)
.map(res => res.json())
.subscribe(books => this.books = books);
}
or something similar depending on what you're using.
could potentially, if using web pack , just require the url
private books: Book[] = require("../../mock/books.json");