Hi Im starting to learn how apis works and got my first stucktrace.
The thing is that Im trying this code
const app = require('express')();
const PORT = 8080;
app.get('/tshirt', (req , res) => {
res.status(200).send({
tshirt: 'blue',
size: 'large'
})
});
But Insomnia show me "Cannot GET /tshirt" and 404 at 'http://localhost:8080/tshirt'
So my question is why this doesnt work?
Additional information:
On firefox I cant run 'http://localhost:8080/tshirt' instead I have to run 'localhost:8080/tshirt'.
But on Insomnia I cant test 'localhost:8080/tshirt' and have to run 'http://localhost:8080/tshirt'.
1.As far as I know, using Promises, it's will your help. please try this.
const getTshirt = async (req, res) => {
res.status(200).send({
tshirt: 'blue',
size: 'large'
})
};
app.get('/tshirt', getTshirt);
Related
first time using React Native with Expo. I saw some tutorials on how to fetch something from locahost but nothing seems to work.
I'm making this simple get request in my backend
app.get("/", async (req, res) => {
res.send("Success");
});
app.listen(5000, () => console.log("Running on 5000"));
On the front end i have
const requestTest = async () => {
try {
const msg = await axios.get("http://192.168.0.29:5000/");
console.log(msg);
} catch (error) {
console.log(error);
}
};
What i tried so far:
Replaced http://localhost:5000/ for my ipv4 address 192.168.0.29, like http://192.168.0.29:5000/.
Since i'm using expo when i start the server it says Metro waiting on exp://192.168.0.29:19000
So i tried to fetch http://192.168.0.29:19000/, also didn't work. After starting the server, i ran this command line adb reverse tcp:5000 tcp:5000, also didn't work.
OBS:
When i connect my app on Expo GO, i'm connecting it through Tunnel, cause if i try to connect it via lan it throws a Network Error.
This is the error I get when I try to retrieve data from IBM Watson Assistant using this code, I am in a React Native environment using axios to request and respond:
//ASSISTANT GREETING
init = async session => {
try
{
const _backendEndpoint = 'https://MYAPPLICATIONNAME.mybluemix.net';
const initialPayload = {
input: {
message_type: 'text',
text: '',
},
};
let response = await axios.post(`${_backendEndpoint}/api/message`, {
...initialPayload,
...session,
});
this.setState({ userSession: session });
this.setState({ text: response.data.output.generic[0].text });
this.setState({ userPayload: response.data });
}
catch (err)
{
console.log('Failed to retrive data from Watson API', err);
}
};
Did IBM Change their codebase or something so you can no longer get requests? I am at a loss.
Thanks
If you are expecting https://MYAPPLICATIONNAME.mybluemix.net to be your instance of Watson Assistant then this is not going to work. The endpoint should look something like https://api.us-south.assistant.watson.cloud.ibm.com
see the API documentation for a list of endpoints for the regions- https://cloud.ibm.com/apidocs/assistant/assistant-v2?code=python#service-endpoint
The endpoint that you use will depend on which region you have your Watson Assistant.
You also need to authenticate with the server, which isn't apparent in your code snippet.
If, however, https://MYAPPLICATIONNAME.mybluemix.net is your backend application server, then I expect it should have an API endpoint defined for /api/message, where your error is occurring. Please share the code for your /api/message endpoint. If you don't have any, then that is why you are getting a 500 error.
I have been trying to solve this issue for the past 2 days and haven't been able to. I've looked this up everywhere and still no solution.. Here's the code:
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());
const PROXY_SERVER_IP = 'IP.IP.IP.IP';
const PROXY_SERVER_PORT = '1234';
const PROXY_USERNAME = 'username';
const PROXY_PASSWORD = 'password';
(async () => {
const browser = await puppeteer.launch({
args: [`--proxy-server=http://${PROXY_SERVER_IP}:${PROXY_SERVER_PORT}`],
});
const page = await browser.newPage();
await page.authenticate({
username: PROXY_USERNAME,
password: PROXY_PASSWORD,
});
await page.goto('https://www.google.ca/', {
timeout: 0,
});
await page.screenshot({ path: 'test4.png', fullPage: true });
await browser.close();
})();
I get a navigation timeout error on the page.goto() call because it just hangs for some reason. I can't figure out why. When I put a proxy that doesn't require authentication, it works. I'm thinking of switching to another headless solution because of this one issue and I would really appreciate some help.
So I figured it out. Turns out the proxy was really bad for some reason. The reason why Axios and cURL gave fast responses was because they just get the initial HTML code and unlike headless browsers, don't actually do anything with HTML text. With headless browsers, they actually make all the requests for the assets as well (css, images, etc.) and any other network requests and it's all going through the proxy, so it's much slower. When I tried a different proxy (one that requires authentication), it was much faster.
I'm working on this express API but when I pass a parameter to test in postman I keep getting this error "Cannot GET /api/v1/profile/psn/sharad0987"
The code is below:
const express = require('express')
const morgan = require('morgan')
const dotenv = require('dotenv')
const app = express()
//load Env
dotenv.config({path: './config.env'})
app.get('api/v1/profile/:platform/:gamertag', function(req, res) {
console.log(req.params.platform, req.params.gamertag);
res.send('Hello')
})
const port = process.env.PORT || 8080
app.listen(port,
console.log(`Server running in ${process.env.NODE_ENV} mode on port ${port}`)
)
Please what am i doing wrong here?
There is a missing leading slash. Looking at routing documentation that should work as soon as it is added. Could you try:
app.get('/api/v1/profile/:platform/:gamertag', function(req, res) {
console.log(req.params.platform, req.params.gamertag);
res.send('Hello');
});
If that is not working, can you update your question with actual postman data you are trying to use
Ok, my mocha tests will pass if I comment out the 'before' and 'after' methods. I am sure that both of my errors are related to each other.
The 'after' method fails stating app.close isn't a function. The 'before' method fails saying it cant find 'app' on my line 7 (clearing server cache).
I am completely out of options or ideas. I would like to be able to start and stop my server at my command. This is the first time that I have attempted to include any type of 'before/after' methods to my mocha testing. working code below, but with my failing portion commented out. Any suggestions??
var request = require('supertest');
var app = require('../../server');
describe('server', function() {
before(function () {
//var app = require('../../server')();
//delete require.cache[require.resolve('app')];
});
after(function () {
//app.close();
});
describe('basic comms', function() {
it('responds to root route', function testSlash(done) {
request(app)
.get('/')
.expect('Content-type', /json/)
//.expect(res.message).to.equal('Hello World!')
.expect(200, done);
});
it('404 everything else', function testPath(done) {
//console.log('testing 404 response');
request(app)
.get('/foo/bar')
.expect(404, done);
});
});
});
In before you require your app in a different way than in line 2. Why would you not use already required app?
Example:
before(function () {
// here you can use app from line 2
});
Regarding app.close, where did you find this function?
Check Express docs:
http://expressjs.com/en/4x/api.html#app
To close express server, you can use this approach:
how to properly close node-express server?