I want to run a JavaScript file that imports scoreService from this file:
import axios from 'axios'
const baseUrl = 'http://localhost:3001/api/scores'
const getAll = () => {
const request = axios.get(baseUrl)
return request.then(response => response.data)
}
const create = (newObject) => {
const request = axios.post(baseUrl, newObject)
return request.then(response => response.data)
}
const update = (id, newObject) => {
const request = axios.put(`${baseUrl}/${id}`, newObject)
return request.then(response => response.data)
}
const scoreService = {getAll, create, update}
export default scoreService
I am unable to use Node to run my file from the terminal because the import statement is not supported by Node. Should or how should I change the import statements to use require instead, or is there a simpler method I could try?
After installing axios:
yourFile.js:
const axios = require('axios');
const getAll = () => {
const request = axios.get(baseUrl)
return request.then(response => response.data)
}
const create = (newObject) => {
const request = axios.post(baseUrl, newObject)
return request.then(response => response.data)
}
const update = (id, newObject) => {
const request = axios.put(`${baseUrl}/${id}`, newObject)
return request.then(response => response.data)
}
export default scoreService = {getAll, create, update};