我可以在Node.js的不同端口上使用变量值吗?



SyntaxError: Cannot use import statement in a module

index.ts

let angle_ne = "Northeast";
export {angle_ne};
export let angle_nw = "Northwest";

index.js

// next 2 line make "SyntaxError : Cannot use import statement outside a module".
// import angle_ne from "./index"
// console.log(angle_ne);
const express    = require('express');
const mysql      = require('mysql');
const dbconfig   = require('./database.js');
const connection = mysql.createConnection(dbconfig);
const app = express();
// configuration =========================
app.set('port', process.env.PORT || 3000);
app.get('/', (req, res) => {
res.send('Root');
});
app.get('/address', (req, res) => {
connection.query('SELECT * from address where Faddress like "%SEOUL%"', (error, rows) => {
if (error) throw error;
// console.log(angle_ne);
console.log('Address info from DB : ', rows);
res.send(rows);

});
});
app.listen(app.get('port'), () => {
console.log('Express server listening on port ' + app.get('port'));
});

在Node.js中创建谷歌地图API相关页面。

索引。ts:它由映射和api相关的编码部分组成,并在localhost:5173上工作。单击位置的地址存储在一个变量中,并显示在console.log中。

我让MySQL连接到localhost:3000。我试图在同一端口上运行API和db,但它有一个不工作API或不工作db的问题。所以我给了不同的端口

我想使用存储在index变量中的值。在index.js中。在目前的状态下有可能吗?如果可能的话,我应该找些什么?或者由于端口不同,无法使用export import导入值?

我预计会出现"东北风";在控制台。我尝试从索引导出。并导入到index.js中语法错误:不能在模块外使用import语句。

我想用这些值创建一个查询。

您的语法似乎不一致。您可以使用require()或es6的导入/导出语法。但不是两者都有。

更多信息在这里:https://www.w3schools.com/nodejs/nodejs_modules.asp

最新更新