How to connect service (class file) with express function res.send() - javascript

I'm using express and i'm trying to call getUser() function, that is inside profileService
class profileService {
constructor() {}
getUser() {
return "I'M A USER";
}
}
module.exports = profileService;
in my routes file
const routes = require("express").Router();
const profileService = require("./services/profile.ts");
routes.get("/", (_req, res) => {
//res.send('this works just fine if it is not commented')
res.send(profileService.getUser());
});
module.exports = routes;
The error that i keep getting is this: TypeError: profileService.getUser is not a function
Here is the package.json, just in case
{
"name": "cuarto-intento",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.20"
},
"dependencies": {
"express": "^4.18.2"
}
}

Related

Uncaught TypeError: Error resolving module specifier “axios”. Relative module specifiers must start with “./”, “../” or “/”. I didn't get the answer

I installed "npm install axios" and imported using "import axios from 'axios'.
I wanted to post the data and get the data
These are the codes I have used.
Package.json :
{
"name": "ceniplex-tickets",
"version": "1.0.0",
"description": "ticket booking app",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Aswin Anilkumar",
"license": "ISC",
"devDependencies": {
"parcel": "^2.8.1",
"parcel-bundler": "^1.8.1"
},
"dependencies": {
"axios": "^1.2.1"
}
}
JS code (Front end) :
import axios from 'axios';
class Model {
constructor() {
this.array = []
}
getArray() {
return this.array;
}
async loadTicketsFromBackend() {
const req = await axios.get('http://localhost:3000/booking')
const booking = req.data;
console.log(booking);
}
async setArray(data) {
const res = await axios.post('http://localhost:3000/booking',data)
return res;
}
}
export default Model;

Cannot import Axios to use with Parcel-Bundler

I am trying to use axios for a http request in my app.js file, but I always get the error message:
Uncaught SyntaxError: Cannot use import statement outside a module.
I created the js folder, files and other js packages using parcel-bundler and run browser-sync to start the server.
What am I missing? Please help I am new to JavaScript. I am on macOS Big Sur 11.5.1, node v14.17.4
import axios from 'axios'
document.addEventListener('DOMContentLoaded', () => {
const form = document.querySelector('form')
form.addEventListener('submit', async (event) => {
event.preventDefault()
const username = document.querySelector('input').value
const response =
await axios.get(`https://api.github.com/users${username}`)
console.log(response.data)
})
})
In the closest (from the root of project directory) package.json file, add or update "type" field with a value of "module". It will ensure that all your .js, .ts and .mjs files are interpreted as ES modules.
...
{
...
"type": "module"
...
}
...
Where can I add it in this file?
{
"name": "githubUser",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1"
}
}
Add "type": "module" to your package.json
{
// ...
"type": "module",
// ...
}
Update:
It should looks like this:
{
"name": "githubUser",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1"
}
}

'Cannot use import statement outside a module' when importing Lit-HTML

I am trying to import Lit-HTML but I get an error: Uncaught SyntaxError: Cannot use import statement outside a module
I used npm to install this library ( version: 6.14.6 )
My index.js ( I have only one javascript file ):
import {render, html, svg} from 'lit-html';
const tasksElement = document.querySelector("#tasks");
const titleInput = document.querySelector("#title");
const descriptionInput = document.querySelector("#description");
const priorityInput = document.querySelector("#priority");
const dateInput = document.querySelector("#date");
const timeInput = document.querySelector("#time");
const submit = document.querySelector("#submit");
My package.json:
{
"name": "timemanagement",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"lit-html": "^1.3.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
The error I get: index.js:1 Uncaught SyntaxError: Cannot use import statement outside a module

Online Chat Application error - Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR

so i'm making an online chat application and when i try to connect i get this message. Im certain that the code is right but i think its a setting on the PC or in the firewall thats blocking my connection. Can anyone help ?
**server.js**- const io = require('socket.io')(3000)
io.on('connection', socket => {
socket.emit('chat-message', 'Hello World')
})
**script.js** - const socket = io('https://localhost:3000')
socket.on('chat-message', data => {
console.log(data)
})
**package.json** - {
"name": "npm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"devStart": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"#types/socket.io": "^2.1.10",
"socket.io": "^2.3.0"
},
"devDependencies": {
"nodemon": "^2.0.4"
}
}

error: can not use import outside a module?

I never used a module outside a boilerplate created by for example create-react-app or angular-cli, now i am trying to mimic modularity without using them, and i get an error: cannot use import outside a module
this is my package.json file:
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel": "^6.23.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0"
}
}
I am using a live-server to create a server for my project, I am failing to do that:
App.js:
const x = 10;
export default x;
index.js:
import x from './App' // can not use import outside a module
The question is what i missing here, what do i really need to create a module??

Categories