I have a simple pages/api/thumbnail.js file with a handler:
export default async function handler(req, res) {
console.dir(req.params)
// ...
}
On a pure express server, it is possible to have a "path parameter", but on NextJS I got a error 404 if a try to pass some path on my API.
Example:
http://localhost:3000/api/thumbnail >> OK, it works
Now with a path:
http://localhost:3000/api/thumbnail/something >> ERROR, 404 not found!
Using NextJS API is it possible to use path parameters or only query string?
PS. I am using Vercel, if using a rewrite is the only solution, its acceptable to me.
You can try adding three dots on the file name like this: pages/api/thumbnail/[[...params]].js Then you can load the params in your handler from req.query
Source: https://nextjs.org/docs/api-routes/dynamic-api-routes
Related
I have frontend running on http://localhost:3000/ and backend running on tomcat 10 in http://localhost:8080/my-app and sometimes backend in http://localhost:8080/your-app. My problem is if i change the backend from my-app to your-app. i dont know how to my frontend can get the basename your-app automatically. i think its very annoying if changing the basename variable in frontend everytimes i change the backend route.
i have try to put basename as env.variable but i want to get the route automatically
You can not find your backend url automaticly with react or javascript tricks.
usualy, I determinate it in public/index.html :
<script>
window.myApp = {
apiBaseURL = "http://localhost:8080/my-app",
}
</script>
and in all Component I have access to it :
function App(){
const baseURL = window.myApp.apiBaseURL;
}
another method define base url in .env file
I'm building bot with discord.js. Everything was working just fine, but when I run code today I got error that TOKEN that I load from .env file is invalid. I checked it and TOKEN is "undefined". My folder tree look like it:
Bot folder
Node.js file (bot.js)
.env file
I'm loading variables with:
const TOKEN = process.env.TOKEN;
In .env file I assign TOKEN like it.
TOKEN=my-bot-token
Any ideas? I was restarting my code editor (VS Code) few times. Exactly the same code worked some time ago...
Thanks!
A very simple approach would just be to type this following command right next to your import statements.
require('dotenv').config();
Then, you can just use your regular forms of accessing the variables. Such as:
process.env.TOKEN
const TOKEN = process.env.TOKEN; does not load anything from your .env. You need to use something like dotenv:
const { parsed, error } = require('dotenv').config();
if (error) {
// Handle error
throw error;
}
console.log(parsed);
Also notice:
As early as possible in your application, require and configure
dotenv.
Edit: You can also set the path to your .env:
const { parsed, error } = require('dotenv').config({
path: '/full/custom/path/to/your/env/vars'
});
I created an app using express generator. I have a js file in my public/javascripts directory. I exported an obj:
module.exports = { my obj here }
Then in my route file (index.js) I've been trying to serve this object as an API so I can fetch it from another js file and do stuff on the front end.
But my require simply won't work. I tried:
var specs = require('./javascripts/specs')
and all variation of the path cause I assumed that my path was wrong.
Am I missing something obvious? I get the following error:
Error: Cannot find module '/javascripts/specs'......
File Structure
https://www.dropbox.com/s/ou9afzckboxbe1w/Screenshot%202018-04-18%2011.59.32.png?dl=0
Your public folder and your routes folder are on the same level in your directory. Therefore you need to go up to the common parent, then down through public/javascripts like this:
var specs = require('../public/javascripts/specs');
Your public/javascript is for client only scripts, if you want to require it from the server you have to provide the full path :
var specs = require('../public/javascripts/specs');
I use grunt-contrib-connect to serve as my front end server(http://127.0.0.1:8888) and express as my backend server(http://127.0.0.1:3000). The front framework is Ember.And I did the cross domain visit by adding Access-Control-Allow-Origin and Access-Control-Allow-Methods to all the req. Now I call:
$.getJSON({
url: http://127.0.0.1:3000/index?callback=?
});
And the /index cgi code is:
router.get('/index', function(req, res) {
var cb = req.params.callback;
res.send(cb({name:'test'}));
});
The browser's console printed an error:
GET http://127.0.0.1:8888/[object%20Object] 404(Not Found)
This wrong url is not my cgi url, how did it exist here?
What can I do to solve this issue?
Thank you
getJSON takes a string, not a hash $.getJSON('http://127.0.0.1:3000/index?callback=foobar');
I've followed this tutorial http://kroltech.com/2013/12/boilerplate-web-app-using-backbone-js-expressjs-node-js-mongodb/ to set up my backend node.js server, but instead of using backbone-marionette for the frontend I want to try ember. Therefore, having successfully set up the server, I am now going through this tutorial for ember http://emberjs.com/guides/getting-started/.
The problem I'm running into is including external templates into my ember routes. With this code and the todos template written in the browser the app works fine.
var Ember = require('ember');
window.Todos = Ember.Application.create();
Todos.Router.map(function() {
this.resource('todos', { path: '/' });
});
However, with a template written in ./templates/application.hbs and using browserify to try this,
Todos.Router.map(function() {
this.resource(require('./templates/application.hbs'), { path: '/' });
});
I get the errors shown below.
Uncaught TypeError: undefined is not a function myapp.js:46841
Error: Assertion Failed: The URL '/' did not match any routes in your application
at new Error (native)
at Error.Ember.Error (http://localhost:3300/js/myapp.js:12978:19)
at Object.Ember.assert (http://localhost:3300/js/myapp.js:12141:11)
at http://localhost:3300/js/myapp.js:47347:15
at invokeCallback (http://localhost:3300/js/myapp.js:22081:19)
at publish (http://localhost:3300/js/myapp.js:21751:9)
at publishRejection (http://localhost:3300/js/myapp.js:22179:7)
at http://localhost:3300/js/myapp.js:30448:7
at Object.DeferredActionQueues.flush (http://localhost:3300/js/myapp.js:18195:24)
at Object.Backburner.end (http://localhost:3300/js/myapp.js:18283:27) myapp.js:15589
Uncaught Error: Assertion Failed: Error: Assertion Failed: The URL '/' did not match any routes in your application
I was hoping someone could shed some light on how to include external templates in an ember router. Thanks!
Thanks for looking into it! I think I did a poor job explaining my question - I understood that it was the template name but I didn't understand how to include that in my application. I searched around a bit more and found grunt-ember-templates.
For future reference, they have really good docs to get you set up, this is what my emberTemplates code looked like.
emberTemplates: {
compile: {
options: {
templateBasePath: 'client/src/templates'
},
files: {
'build/templates.js': ['client/src/templates/*.hbs']
}
}
},
And then I just added 'application' as my route template name.
Don't forget to compile all of build/ in your app build.